srand

How can I store the state of the pseudo-random generator in Perl?

为君一笑 提交于 2019-12-10 02:15:49
问题 Is there a way to store the current state of the built in pseudo-random number generator in Perl so that when my program is run again, it can pick up the sequence from where it left off rather than starting with a new sequence? Right now, I am storing where I am as well as the initial seed and then throwing away the initial segment which I have already seen using something similar to: sub consume_upto_n { my ($seed, $n) = @_; $n = 1 unless defined $n and $n >= 1; srand $seed; rand for 1 .. $n

Creating and managing two independent random number sequences

谁说胖子不能爱 提交于 2019-12-08 08:01:42
问题 I'm having trouble generating two independent random sequences using the rand and srand functions. The details are below, any help would be most appreciated. I'm working on a puzzle game for the iPhone, and usually for the random number generation I use the arc4 function. However for the multiplayer mode I want both players to have the same pieces throughout the game, and the only way I can control it is to have two repeatable random sequences. If I then send the seeds to the other device,

Calling srand() twice in the same program [closed]

一世执手 提交于 2019-12-07 13:41:27
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . why is it when i call srand() at 2 very different points it cause numbers to not be random? Once i remove one of them it goes back to normal. 回答1: It depends on how you call it. The purpose of srand() is to seed the pseudo-random number generator used by rand() . So when you call srand(i) , it will initialise

c++ srand does not give same sequences of random numbers

旧城冷巷雨未停 提交于 2019-12-07 04:26:28
问题 I have a optimisation algorithm which uses rand() and srand(). In order to be able to test the behaviour I have set the seed to a specific number in order to get the same sequence of random numbers on different runs of the program. #define RN rand()/(RAND_MAX+1.0) int main(int argc, char **argv) { unsigned int seed=47456536; srand(seed); // a lot of stuff including RN } Issue is that in different runs I get different sequence of numbers. Is that possible? 回答1: First off: Do not use rand.

【C++】srand及rand函数

本秂侑毒 提交于 2019-12-07 03:40:49
PS:是在数据挖掘实验中遇到的 摘自:【 http://blog.csdn.net/candyliuxj/article/details/4396666 】 --candyliuxj rand 函数名: rand 功 能: 随机数发生器 用 法: int rand(void); 所在头文件: stdlib.h 函数说明 : rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在一定 的范围里可看成是随机的。 rand()返回一随机数值的范围在0至RAND_MAX 间。RAND_MAX的范围最少是在32767之间(int)。用 unsigned int 双字节是65535,四字节是4294967295的整数范围。0~RAND_MAX每个数字被选中 的机率是相同的。 用户未设定随机数种子时,系统默认的随机数种子为1。 rand()产生的是伪随机数字,每次执行时是相同的;若要不同,用函数srand()初始化它。 程序例包含的内容: #include <stdlib.h> #include <time.h> #define MIN 1 //随机数产生的范围 #define MAX 10 srand((unsigned)time(0)) MIN + ( int )MAX * rand() / (RAND_MAX + 1) srand 函数名: srand 功 能:

Calling srand() twice in the same program [closed]

你离开我真会死。 提交于 2019-12-05 22:43:10
why is it when i call srand() at 2 very different points it cause numbers to not be random? Once i remove one of them it goes back to normal. It depends on how you call it. The purpose of srand() is to seed the pseudo-random number generator used by rand() . So when you call srand(i) , it will initialise rand() to a fixed sequence which depends on i . So when you re-seed with the same seed, you start getting the same sequence. The most common use case is to seed the generator just once, and with a suitable "random" value (such as the idiomatic time(NULL) ). This guarantees makes it likely that

Usefulness of `rand()` - or who should call `srand()`?

本秂侑毒 提交于 2019-12-04 22:12:05
Background: I use rand() , std::rand() , std::random_shuffle() and other functions in my code for scientific calculations. To be able to reproduce my results, I always explicitly specify the random seed, and set it via srand() . That worked fine until recently, when I figured out that libxml2 would also call srand() lazily on its first usage - which was after my early srand() call. I filled in a bug report to libxml2 about its srand() call , but I got the answer: Initialize libxml2 first then. That's a perfectly legal call to be made from a library. You should not expect that nobody else calls

Is it modern C++ to use srand to set random seed?

时光毁灭记忆、已成空白 提交于 2019-12-04 17:28:13
问题 For code that uses std::random_shuffle , I need to set a random seed so that the pseudorandom sequences produced vary in each program run. The code example here makes a call to srand ( unsigned ( time (NULL) ) ); which needs to #include <ctime> #include <cstdlib> I wonder: Since C++11 includes major updates to pseudorandom number generation, is this still up to date? What should I use to set the random seed for std::random_shuffle ? 回答1: random_shuffle uses an implementation-defined random

Avoiding Repeated seed generation using srand()

[亡魂溺海] 提交于 2019-12-04 05:55:28
问题 I have a typical situation where I need to generate a batch of random numbers. I have used a loop which generates 100 random numbers on each pass: for(int i=0; i<npasses; i++) { srand(time(NULL)); //Initialize seed for(int j=0; j<100; j++) printf("%d ", rand()%10); printf("\n"); //New line after 100 numbers } Now, the inner loop executes in less than a millisecond. As a result, there is no change in the value of time(). This re-initializes the seed (srand()) to the same value and my random

rand() return the same number [duplicate]

谁说我不能喝 提交于 2019-12-04 04:44:13
问题 This question already has answers here : srand() — why call it only once? (7 answers) Closed 2 years ago . I am making a simple example in C with rand() but the function always return the same number despite i am using srand(). This is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> int generate(int min, int max) { srand(time(NULL)); return rand() % (max - min + 1) + min; } int main() { int i; for (i = 0; i < 10; i++) { printf("Number random %d = %d\n", i, generate(1, 100))