srand

Is it good idea to pass uninitialized variable to srand?

梦想的初衷 提交于 2019-12-04 04:30:58
问题 Is it good idea to pass uninitialized variable to srand instead of result of time(NULL) ? It is one #include and one function call less. Example code: #include <stdlib.h> int main(void) { { usigned seed; //uninitialized srand(seed); } //other code return 0; } instead of #include <stdlib.h> #include <time.h> int main(void) { srand(time(NULL)); //other code return 0; } 回答1: No, it isn't. Reading an uninitialized value results in undefined behavior. It can be zero, it can be semi-random — but as

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

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 - 1 ; return ; } For example:

Problems with srand(), C++

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to write a program that generates a pseudorandom numbers using a seed. However, I'm running into problems. I get this error 39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be Using this code #include <iostream> #include <iomanip> #include <sstream> #include <limits> #include <stdio.h> using namespace std ; int main(){ int rand_int; string close ; close == "y" ; cout << endl << endl ; cout << "\t ___________________________________" << endl ; cout << "\t| |" << endl ; cout << "\t| Pseudorandom Number Game! |" <<

Is it good idea to pass uninitialized variable to srand?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it good idea to pass uninitialized variable to srand instead of result of time(NULL) ? It is one #include and one function call less. Example code: #include <stdlib.h> int main(void) { { usigned seed; //uninitialized srand(seed); } //other code return 0; } instead of #include <stdlib.h> #include <time.h> int main(void) { srand(time(NULL)); //other code return 0; } 回答1: No, it isn't. Reading an uninitialized value results in undefined behavior. It can be zero, it can be semi-random ― but as such, it can repeatedly be the same value. It may

C++随机数笔记

旧城冷巷雨未停 提交于 2019-12-03 01:52:11
版权声明:本文为CSDN博主「candyliuxj」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接: https://blog.csdn.net/candyliuxj/article/details/4396666 一、rand()和srand()的关系   rand()和srand()要一起使用,其中srand()用来初始化随机数种子,rand()用来产生随机数。   因为默认情况下随机数种子为1,而相同的随机数种子产生的随机数是一样的,失去了随机性的意义,所以为使每次得到的随机数不一样,用函数srand()初始化随机数种子。srand()的参数,用time函数值(即当前时间),因为两次调用rand()函数的时间通常是不同的,这样就可以保证随机性了。 二、产生一定范围随机数的通用表示公式   要取得[a,b)的随机整数,使用 (rand() % (b-a))+ a (结果值含a不含b)。   要取得[a,b]的随机整数,使用 (rand() % (b-a+1))+ a (结果值含a和b)。   要取得(a,b]的随机整数,使用 (rand() % (b-a))+ a + 1 (结果值不含a含b)。   (总的来说,通用公式: a + rand() % n ;其中的a是起始值,n是整数的范围)    要取得a到b之间的随机整数

rand()与 srand()

匿名 (未验证) 提交于 2019-12-03 00:39:02
  srand()就是给rand()提供种子seed。   在C语言中,srand()与rand()是随机函数,其中srand函数是伪随机数发生器的初始化函数,原型void srand(unsigned int seed);rand()是伪随机数生成器,在调用了srand()产生的种子以后,产生伪随机数。   C语言中语句srand( (time(NULL) ) ; 表示设置一个随机种子,每次运行都能保证随机种子不同。   在C语言中,rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,它是根据一个数,我们可以称它为种子,为基准以某个递推公式推算出来的一系数,但这不是真正的随机数,当计算机正常开机后,这个种子的值是定了的,除非你破坏了系统,为了改变这个种子的值。因此,C提供了srand()函数,它的原型是 void srand( int a)。用来改变这个种子值。   srand( (time(NULL) )中time(NULL)函数是得到一个从1900年1月1日到现在的时间秒数,这样每一次运行程序的时间的不同就可以保证得到不同的随机数了    #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); printf( " %d "

srand(time(null)) on iPhone don't work =(

吃可爱长大的小学妹 提交于 2019-12-02 18:15:29
问题 I need generate random numbers in my iPhone game. I using rand() function. But it isn't enough random. I tried using srand with time(Null). But my random generator was periodic. /dev/random isn't an answer because I need new random number each 0.1 sec. 回答1: On iOS and OS X, use arc4random instead. Higher quality randomness, and no need to worry about seeding. However, rand() shouldn't be noticably periodic, unless you're calling srand each time around. Or using the random numbers incorrectly.

srand is too slow in C++ it return same number

半世苍凉 提交于 2019-12-02 14:24:01
I'm just trying to have simply RndInt(limit) function that will return random numbers with a limit as limit. cout << "Enter higher limit of random range:" ; cin >> limit; while(limit != 0) { // RndInt(limit); cout << "Random number smaller than " << limit << " is " << RndInt(limit) << endl; cin.get(); cin.get(); cout << "Other random number smaller than " << limit << " is " << RndInt(limit) << endl; cin.get(); cin.get(); cout << "There is " << RndInt(limit) << " ways I love you" <<endl ; cout << "Enter higher limit of random range:" ; cin >> limit; } return 0; } int RndInt(int limit) { srand

srand(time(null)) on iPhone don't work =(

你说的曾经没有我的故事 提交于 2019-12-02 12:34:57
I need generate random numbers in my iPhone game. I using rand() function. But it isn't enough random. I tried using srand with time(Null). But my random generator was periodic. /dev/random isn't an answer because I need new random number each 0.1 sec. On iOS and OS X, use arc4random instead. Higher quality randomness, and no need to worry about seeding. However, rand() shouldn't be noticably periodic, unless you're calling srand each time around. Or using the random numbers incorrectly. You could try random() / srandom() instead. Better generator than rand() / srand(). Are you building for

Problems with srand(), C++

余生颓废 提交于 2019-12-02 11:25:19
I'm trying to write a program that generates a pseudorandom numbers using a seed. However, I'm running into problems. I get this error 39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be Using this code #include <iostream> #include <iomanip> #include <sstream> #include <limits> #include <stdio.h> using namespace std ; int main(){ int rand_int; string close ; close == "y" ; cout << endl << endl ; cout << "\t ___________________________________" << endl ; cout << "\t| |" << endl ; cout << "\t| Pseudorandom Number Game! |" << endl ; cout << "\t|___________________________________