是否有使用播种随机数生成的方法吗? [英] Is there an alternative to using time to seed a random number generation?

查看:95
本文介绍了是否有使用播种随机数生成的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一段code的多个实例(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 指令是pretty可靠(随机)种子。

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天全站免登陆