srand

Rand() % 14 only generates the values 6 or 13

时光怂恿深爱的人放手 提交于 2020-01-18 04:44:31
问题 Whenever I run the following program the returned values are always 6 or 13. #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> using namespace std; //void randomLegs(); //void randomPush(); //void randomPull(); //void randomMisc(); int main(int argc, const char * argv[]) { srand(time(NULL)); //randomLegs(); cout << rand() % 14; return 0; } I have run the program close to a hundred times during today and yesterday. Can anyone tell me what I'm doing wrong? Thank you.

rand() with srand() is giving strangely similar results. The return from rand() is very similar

﹥>﹥吖頭↗ 提交于 2020-01-15 07:51:07
问题 This is a seemingly common question, so I hope I don't sound redundant. But the range returned from rand() should be between 0 and RAND_MAX, however, when I do a very simple rand statement, I'm always getting returns within a very small range. This range is something like 1,4XX,XXX,XXX. I figured this might be a clock thing, so I waited thirty minutes and I'm still getting numbers in the same range. Here is some sample output from twenty minutes ago: Matthews-Macbook-Pro:Data_Structures

在给定范围中取不重复的随机数

六眼飞鱼酱① 提交于 2020-01-15 02:03:35
在给定范围中取不重复的随机数 随机取m个数(在1到n的范围之内),(m<=n),要求m个数没有重复。有没有什么好的算法,时间复杂度和空间复杂度都很好? 方法一:用STL中的set集,红黑树来处理 取随机数可以用C++标准的rand,至于M个不重复,用std::set来解决,把取到的随机数插入到set里面,通过set的size()==m来判断是否已取够m个了。 #include <set> #include <stdlib.h> int main() { std::set<int> s; while(1) { int r = rand()%n; s.insert(r); if(s.size() == m) { break; }//if }//while } 由于set底层实现是红黑树,插入复杂度是对数级。 方法二: #include <iostream> #include <cstdlib> //用于rand()和srand()函数 #include <ctime> //设置不同的随机数 using namespace std; int main (){ srand( time( 0 ) ); //调用不重复的随机数函数 unsigned i; for ( int n = 0; n++ < 10; ) { i = rand() ; //对i 赋系统的随机数 cout << " The

srand()与rand()生成随机数

天大地大妈咪最大 提交于 2020-01-10 19:39:32
srand()与rand()生成随机数 经过测试,当srand的值确定时,其对应的rand值也是确定的。 #include <iostream> using namespace std; int main() { for (int i = 0; i <= 10; i++) { srand(i); cout << "当seed = " << i << "时,其结果为:" << rand() << endl; } cout << endl << endl << "------第二次验证---------" << endl << endl; for (int i = 0; i <= 10; i++) { srand(i); cout << "当seed = " << i << "时,其结果为:" << rand() << endl; } } 来源: https://www.cnblogs.com/onetrainee/p/12177659.html

rand() seeded the same way generates different results

放肆的年华 提交于 2020-01-06 08:01:31
问题 I am developing an application in PHP and C but the result of rand is different between the two languages, even though I am using the same seed: PHP: srand(1); $random = rand(); // returns 32422 C: srand(1); int random = rand(); // returns 41 Why is this happening? 回答1: There is more than one way to implement a pseudo-random number generator. Every programming language is free to specify its own rand implementation, or even to specify nothing. For example, the C specification only says that

rand() seeded the same way generates different results

允我心安 提交于 2020-01-06 08:01:02
问题 I am developing an application in PHP and C but the result of rand is different between the two languages, even though I am using the same seed: PHP: srand(1); $random = rand(); // returns 32422 C: srand(1); int random = rand(); // returns 41 Why is this happening? 回答1: There is more than one way to implement a pseudo-random number generator. Every programming language is free to specify its own rand implementation, or even to specify nothing. For example, the C specification only says that

C++ generating random numbers-1

戏子无情 提交于 2020-01-06 05:48:04
问题 int main() { srand((unsigned)time(0)); int random_integer; int lowest=0, highest=10; int range=(highest-lowest)+1; for(int index=0; index<20; index++){ random_integer = (rand() % range) + lowest/(RAND_MAX + 1.0); cout << random_integer << endl; } } I am getting the output from 0 to 10, 11 numbers, but I don't want to get number 10, just numbers 0 to 9, that means 10 random numbers, what should I do? 回答1: Modulo operation x % c; returns the remainder of division of number x by c . If you do x

rand和srand的用法

独自空忆成欢 提交于 2019-12-28 01:21:48
文章目录 rand和srand 代码 atoi 用time来作为传入srand的参数 取余符号 % rand和srand 在man手册里面有讲述到,rand,rand_r和srand 都是与这个pseudo-random number generator有关的 伪随机数字发生器,就是产生随机数的lor SYNOPSIS #include <stdlib.h> int rand(void); int rand_r(unsigned int *seedp); void srand(unsigned int seed); 原型是上面那样描述的。 DESCRIPTION The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the

Random numbers that do not match each other

ⅰ亾dé卋堺 提交于 2019-12-25 05:03:25
问题 I want to produce different numbers with C . We can generate a random number using the stdlib library and the srand function. For example; I want to produce a random number between 0 and 5. #include <stdio.h> #include <time.h> #include <stdlib.h> int main(void) { int i; int n = 4; int array[3]; srand(time(NULL)); for(i = 0; i < n; i++) { array[i] = rand() % 5; printf("%d\n", array[i]); } return 0; But the same numbers may coincide here.Like this: 2 4 4 1 How can I prevent this? 回答1: Maybe you

rand () for c++ with variables

家住魔仙堡 提交于 2019-12-25 04:53:21
问题 int userHP = 100; int enemyHP = rand() % ((userHP - 50) - (userHP - 75)) + 1; okay, for some reason this doesnt seem to work right, im trying to get 50 -25 hp for enemys. also id rather it be a percentage... like int enemyHP = rand() % ((userHP / 50%) - (userHP / 75%)) + 1; but id like to stick with integers and not mess with floats or doubles... can someone help me? 回答1: Perform some algebra on this: rand() % ((userHP - 50) - (userHP - 75)) + 1; rand() % (userHP - 50 - userHP + 75) + 1; rand