Hi, I am teaching myself C++ and have a question on something. I have random numbers being generated in my code however it always starts off with the same number. It will randomly generate but I am not sure what I am doing wrong. I am using Visual C++ for the compiler This is a random phrase generator that outputs a phrase depending on what number you input. It asks for 1-9. If you input a number higher than 9 is where I randomly generate a number for you. This issue was that I could not keep the constant C to 10 because it would always generate 1 first so I made the constant C = 100 and created a function to test what number was in C and scale it down from there. The issue is, now it always starts with the same number... What am I doing wrong? I have since taken the random number generator out and just used the function alone which provides desired effects however I would like to know why random number generator is not working properly. I used the same number generator in a simplified roulette game I wrote and it works perfectly. Is it because it is in an if statement and it only gets executed once every time the if statement is read? If so, then why does it choose around the same number every time the program is executed? [SIZE=2][COLOR=#008000]// arrays example [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<iostream> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<sstream> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<ctime> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][SIZE=2] std; [/SIZE][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] c = 100; string phrase[c] = { [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"You are the greatest!"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Nobody could ask for better than you!"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"You are the best!"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Thank you for always being there for me."[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"I love you!"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"You are the light of my life!"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"BoninMcScribbleNugget"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Randomly generated nothingness"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Who gives a berry!" [/COLOR][/SIZE][SIZE=2]}; [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] a = 0; string b; [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] checkme([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] a); [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] main () { srand(([/SIZE][SIZE=2][COLOR=#0000ff]unsigned[/COLOR][/SIZE][SIZE=2])time(NULL)); [/SIZE][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2](;(a>9) || (a==0);) { cout<<[/SIZE][SIZE=2][COLOR=#800000]"Your current number is: "[/COLOR][/SIZE][SIZE=2]<<a<<endl;; cout<<[/SIZE][SIZE=2][COLOR=#800000]"Please enter a number 1 - 9: "[/COLOR][/SIZE][SIZE=2]; getline(cin,b); stringstream(b)>>a; [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2](a>9) { a = ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2]) c * rand() / (RAND_MAX+ 1.0); [/SIZE][SIZE=2][COLOR=#008000]// a = (int) a * rand() / (RAND_MAX + 1.0); [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]// srand((unsigned)time(NULL)); [/COLOR][/SIZE][SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#800000]"You have entered a number greater than 9.\nWe shall choose a number for you.\n"[/COLOR][/SIZE][SIZE=2]; [/SIZE][SIZE=2][COLOR=#008000]/* switch(variable) { case value: statement; statement; break; case value: statement; statement; break; default: statement; statement; break; } //switch */ [/COLOR][/SIZE][SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#800000]"The number being randomly generated is: "[/COLOR][/SIZE][SIZE=2]<<a<<endl; system([/SIZE][SIZE=2][COLOR=#800000]"pause"[/COLOR][/SIZE][SIZE=2]); a = checkme(a); cout<<[/SIZE][SIZE=2][COLOR=#800000]"The number we have chosen is "[/COLOR][/SIZE][SIZE=2]<<a<<endl; } [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2](a == 0) { cout<<[/SIZE][SIZE=2][COLOR=#800000]"Zero is not an option.\n"[/COLOR][/SIZE][SIZE=2]; } } cout<<[/SIZE][SIZE=2][COLOR=#800000]"Your number is: "[/COLOR][/SIZE][SIZE=2]<<a<<endl; cout<<[/SIZE][SIZE=2][COLOR=#800000]"Your randomly generated phrase is:\n\n"[/COLOR][/SIZE][SIZE=2]; b = phrase[a]; cout<<b<<endl; system([/SIZE][SIZE=2][COLOR=#800000]"pause"[/COLOR][/SIZE][SIZE=2]); [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] 0; } [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] checkme([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] a) { [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=11) && (a<=20)) a = 1; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=21) && (a<=30)) a = 2; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=31) && (a<=40)) a = 3; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=41) && (a<=50)) a = 4; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=51) && (a<=60)) a = 5; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=61) && (a<=70)) a = 6; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=71) && (a<=80)) a = 7; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=81) && (a<=90)) a = 8; [/SIZE][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2]((a>=91) && (a<=100)) a = 9; [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] a; } [/SIZE] Code (markup):
You might find the comments on rand() and srand() on the following site useful: http://www.daniweb.com/techtalkforums/thread1769.html'
Thanks for the article. That did help me out in 1 aspect. I was unclear on whether or not I had to put srand(); in the if statement or in mainline. It is definitely mainline although I still have the same issue. I learned some things from the comments section as well which is nice. Thanks again but I still have the same issue. Wayne
I played with the problem on Borland C++ and came up with the following hack, which will generate a more random result: srand((unsigned)time(NULL)); a = (int) c * rand() / (RAND_MAX+ 1.0); for ( int b=a; b > 0; b--) { a = (int) c * rand() / (RAND_MAX+ 1.0); } Code (markup): There are a lot of discussions about randomness around the internet and in the archives at Dr. Dobbs Journal. The problem is coming up with a truly random seed. People use time because it is always changes every second. This helps make the random number generator look more random. I recall reading somewhere, many years ago, that the first number in a random sequence is never random. The hack above tries to deal with that problem by iterating "n" times through the generator.
rand() is called psuedo-random generator. Given the same seed, it will always generate the same sequence of random numbers. Your seed is set by srand function. So you want to give it an input that is always changing.. Clancey above has given you the most famous method to do this.. i.e. give current time as seed.
OK, so call me an idiot, but doesn't the code in the original post have him seeding from time already?
The range for random values (c) must be multiplied after the random number is fit into [0,1). You are also typecasting c only, not the entire expression. Change: to: a = (int)(rand() / (RAND_MAX + 1.0) * c); Code (markup):