惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

我心向阳

基于 typecho 的百度足迹地图插件分享 春风吹向了百度网盘 Linux 版 五一武汉行 ubuntu2604 尝鲜 涟水河畔有故事 给我的QQ机器人取名-openclaw初探即终结 这次飞牛动作有点大 升级 typecho1.3.0 的那点事 我离交通事故仅差1毫米 打卡湘潭小谷吖循环书仓 优化高亮代码外观
有趣的随机种子
ymz316 · 2026-01-12 · via 我心向阳

看惯了明暗主题,今天我突然想实现一下更加丰富的页面颜色。

我的想法是:每天随机获取一个背景图片,然后稍加处理让其适应明暗主题。

随机获取简单,可若每天获取同样的图片,对于我来说首先想到的是给个时间判断,或者说是缓存键值。可是我又不想再去写一段判断的代码了,于是用 ai 搜索了一下,竟然发现了一个叫做随机种子的东西,于是乎,就这么容易的实现了。

一、之前的随机获取图片代码

function getRandImage()
{
    $img_array = glob(dirname(__FILE__) . '/images/bgdemo/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);

    // if (count($img_array) == 0) die();
    $rand_img = basename($img_array[array_rand($img_array)]);
    echo Helper::options()->themeUrl . '/images/bgdemo/' . $rand_img;
}

二、加入是否使用随机种子的判断

function getRandImage(bool $srand = false)
{
    $img_array = glob(dirname(__FILE__) . '/images/bgdemo/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);

    if (empty($img_array)) {
        return; // 或抛出错误
    }

    if ($srand == true) {
        // 基于日期的随机种子
        $seed = (int) date('Ymd');
        mt_srand($seed);
        $index = mt_rand(0, count($img_array) - 1);
        mt_srand(); // 重置随机数生成器
    } else {
        // 使用 array_rand() 直接随机选择
        $index = array_rand($img_array);
    }
    $rand_img = basename($img_array[$index]);
    return Helper::options()->themeUrl . '/images/bgdemo/' . $rand_img;
}

三、在需要使用随机种子的地方调用

ps:css 使用的是 tailwindcss 的通用类

    <!-- 模糊图片背景 -->
    <div class="fixed inset-0 bg-no-repeat bg-center brightness-150 dark:brightness-50 -z-10 opacity-40 dark:opacity-40" style="background-image: url(<?php echo getRandImage(1); ?>); background-size: 10000%;">
        <div class="fixed inset-0 backdrop-blur-xl"></div>
    </div>

然后我的网站每天就会随机从图库中获取一张背景图了。今天的首页效果如下,期待明天能获得一张更加亮丽的背景图。

&#25130;&#22270; 2026-01-12 14-45-06.webp

四、介绍一下php的随机种子

这里其实用到了2个函数,mt_rand()mt_srand() ,他们是 PHP 中基于 Mersenne Twister 算法(一种高质量、快速的随机数生成算法)的生成伪随机数的一对函数。

mt_srand() 用于手动设置随机数生成器的种子(seed)。

mt_srand($seed); // $seed 是一个整数

mt_rand() 用于生成一个伪随机整数。

mt_rand();          // 返回 0 到 mt_getrandmax() 之间的整数
mt_rand($min, $max); // 返回 $min 到 $max 之间的整数(含边界)
注意:如果不设置种子(seed),PHP 会在首次调用时自动初始化一个种子(通常基于系统时间或熵源),之后每次运行脚本都会产生不同的序列。