srand

Fill a vector with random numbers c++

梦想的初衷 提交于 2019-11-28 21:32:13
I've got a vector that I'm trying to fill up with random numbers. I keep running into an issue however that the vector mostly outputs 0 each time that I'm running it (it shouldn't output a 0 ever). What am I doing wrong in my code written below to make it output 0 (it outputs 0 more so than any other number): vector<int> myVector; srand((unsigned)time(NULL)); int a = rand() % 20 + 1; //1 to 20 for (int i =0; i < a; i++){ int b = rand() % 20 + 1; myVector.push_back(b); cout << myVector[b] << endl; } I am a beginner and have not done much C++ programming in a long time so I'm not sure what is

C语言随机数

夙愿已清 提交于 2019-11-28 17:53:53
在C语言编程中,经常会用到随机数,所以总结一下rand()和srand()两个随机数的生成函数 rand()会生成一个位于0~RAND_MAX之间的整数;rand()函数产生的随机数是伪随机数,是根据一个数值按照某个公式推算出来的,这个数值称之为 “种子”。种子和随机数之间的关系是一种正太分布,种子在每次启动计算机时是随机的,但是一旦计算机启动以后它就不再变化了;也就是说,每次启动计算机以后,种子就是定值了,所以根据公式推算出来的结果(也就是生成的随机数)就是固定的。 下面是一个随机数生成的实例: include <stdio.h> include <stdlib.h> int main(){ int a = rand(); printf("%d\n",a); return 0; } srand()函数是根据时间作为参数,只要每次播种的时间不同,那么生成的种子就不同,最终的随机数也就不同。使用 <time.h> 头文件中的 time() 函数即可得到当前的时间(精确到秒),就像下面这样: srand((unsigned)time(NULL)); 对上面的代码进行修改,生成随机数之前先进行播种: include <stdio.h> include <stdlib.h> include <time.h> int main() { int a; srand((unsigned)time

How can I generate different random numbers for each player?

女生的网名这么多〃 提交于 2019-11-28 14:52:43
Both players get the same random number! ّI want each player to get a different number since they are throwing dice. here is the code: #include <stdlib.h> #include <stdio.h> #include <time.h> int roll_a_dice(void); int main(int argc, const char *argv[]) { int flag; int answer1 = roll_a_dice(); int answer2 = roll_a_dice(); printf("Die 1 (rolled by player 1): %d\n", answer1); printf("Die 2 (rolled by player 2): %d\n", answer2); if (answer1>answer2) { printf("Player 1 is starting!\n"); flag = 1; } else { printf("Player 2 is starting!\n"); flag = 2; } printf("Goodbye!\n"); return 0; } int roll_a

generate reliable pseudorandom number

夙愿已清 提交于 2019-11-28 09:48:08
问题 I want to write a multiplayer game on iOS platform. The game relied on random numbers that generated dynamically in order to decide what happen next. But it is a multiplayer game so this "random number" should be the same for all device for every player in order to have a consistent game play. Therefor I need a good reliable pseudorandom number generator that if I seed it a same number first than it will keep generate same sequences of random number on all device (iPad/iPhone/iPodTouch) and

random_shuffle not really random

依然范特西╮ 提交于 2019-11-28 07:19:23
问题 I'm using the random_shuffle on a vector like this: #include <algorithm> vector <Card> deck; //some code to add cards to the deck here random_shuffle ( deck.begin(), deck.end() ); When run, the content of the deck is mixed up, but this mixed-up order is kept when I restart the program. Did I miss something? How can I make it truly random? 回答1: You need to seed the psuedo-random number generator first using srand. #include <algorithm> #include <cstdlib> ... std::srand(std::time(0)); vector

Random numbers in C

做~自己de王妃 提交于 2019-11-28 01:49:47
for(i = 0; i < n; i++){ srand(time(NULL)); printf("%d ", time(NULL)); for(j = 0; j < (n-1); j++){ a[i][j] = rand(); } } I try to generate random numbers, but they are the same... I try srand(i * time(NULL)) . No matter.. What should i do? Array declaration: int** a; int i; printf("Enter array size: "); scanf("%d", &n); a = (int**)calloc(n, sizeof(int)); for(i = 0; i < n; i++) a[i] = (int*)calloc(n-1, sizeof(int)); Call srand() outside of the loop. You are reseeding it every iteration. srand() seeds the random number generator so you get a different sequence of random numbers depending on the

How does calling srand more than once affect the quality of randomness?

回眸只為那壹抹淺笑 提交于 2019-11-28 01:15:20
This comment , which states: srand(time(0)); I would put this line as the first line in main() instead if calling it multiple times ( which will actually lead to less random numbers ). ...and I've bolded the line which I'm having an issue with... repeats common advice to call srand once in a program. Questions like srand() — why call only once? re-iterate that because time(0) returns the current time in seconds, that multiple calls to srand within the same second will produce the same seed. A common workaround is to use milliseconds or nanoseconds instead. However, I don't understand why this

Fill a vector with random numbers c++

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 20:58:08
问题 I've got a vector that I'm trying to fill up with random numbers. I keep running into an issue however that the vector mostly outputs 0 each time that I'm running it (it shouldn't output a 0 ever). What am I doing wrong in my code written below to make it output 0 (it outputs 0 more so than any other number): vector<int> myVector; srand((unsigned)time(NULL)); int a = rand() % 20 + 1; //1 to 20 for (int i =0; i < a; i++){ int b = rand() % 20 + 1; myVector.push_back(b); cout << myVector[b] <<

How can I generate different random numbers for each player?

不问归期 提交于 2019-11-27 19:44:05
问题 Both players get the same random number! ّI want each player to get a different number since they are throwing dice. here is the code: #include <stdlib.h> #include <stdio.h> #include <time.h> int roll_a_dice(void); int main(int argc, const char *argv[]) { int flag; int answer1 = roll_a_dice(); int answer2 = roll_a_dice(); printf("Die 1 (rolled by player 1): %d\n", answer1); printf("Die 2 (rolled by player 2): %d\n", answer2); if (answer1>answer2) { printf("Player 1 is starting!\n"); flag = 1;

How often should I call srand() in a C++ application?

泪湿孤枕 提交于 2019-11-27 16:03:19
I have a C++ application which calls rand() in various places. Do I need to initialize srand() regularly to ensure that rand() is reasonably random, or is it enough to call it once when the app starts? If you have only a single thread, seed once . If you reseed often, you might actually break some of the statistical properties of the random numbers. If you have multiple threads, don't use rand at all, but rather something threadsafe like drand48_r , which lets you maintain a per-thread state (so you can seed once per thread). Only once, at the start of your application. No just calling once is