除了使用时间来生成随机数之外,还有其他选择吗? [英] Is there an alternative to using time to seed a random number generation?

查看:25
本文介绍了除了使用时间来生成随机数之外,还有其他选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在计算集群中同时运行一段代码的多个实例(2000 个左右).它的工作方式是我提交作业,集群将在节点经常打开时运行它们,每个节点有几个作业.这似乎为使用时间种子的随机数生成中的大量实例生成了相同的值.

I'm trying to run several instances of a piece of code (2000 instances or so) concurrently in a computing cluster. The way it works is that I submit the jobs and the cluster will run them as nodes open up every so often, with several jobs per node. This seems to produce the same values for a good number of the instances in their random number generation, which uses a time-seed.

有没有我可以使用的简单替代方法?可重复性和安全性并不重要,快速生成独特的种子才是重要的.什么是最简单的方法,如果可能的话,跨平台方法会很好.

Is there a simple alternative I can use instead? Reproducibility and security are not important, quick generation of unique seeds is. What would be the simplest approach to this, and if possible a cross platform approach would be good.

推荐答案

rdtsc 指令是一个非常可靠(且随机)的种子.

The rdtsc instruction is a pretty reliable (and random) seed.

在 Windows 中,它可以通过 __rdtsc() 内部函数访问.

In Windows it's accessible via the __rdtsc() intrinsic.

在 GNU C 中,可以通过以下方式访问:

In GNU C, it's accessible via:

unsigned long long rdtsc(){
    unsigned int lo,hi;
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((unsigned long long)hi << 32) | lo;
}

该指令测量自处理器开机以来的总伪周期.鉴于当今机器的高频率,即使两个处理器同时启动并以相同的速度运行,也极不可能返回相同的值.

The instruction measures the total pseudo-cycles since the processor was powered on. Given the high frequency of today's machines, it's extremely unlikely that two processors will return the same value even if they booted at the same time and are clocked at the same speed.

这篇关于除了使用时间来生成随机数之外,还有其他选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆