I'm very new to C++ and I have been watching tutorials and reading about it. After learning about random numbers in C++ I decided to make a simple rock, paper, scissors program. A few hours later and it's done, at the end it asks if the user wants to play again, and it starts over, but the only problem is it's using the same number from the last round, thus putting out the same thing each time. So, is there a way I can reset my int x?
Yes, you're going to want to #include <ctime> and use the function time.
The statement int randomNumber = rand() + time(0);
will generate a new number for the variable randomNumber every time the program is run.
(You can use the modulus operator if you want randomNumber to be in a certain domain...).
rand() % time() is mostly just really weird. Though I suppose it will produce the desired effect... kinda.
I wouldn't recommend doing it because it doesn't change the stream of numbers produced by rand, it only offsets them. Which makes the numbers more predictable than properly seeding.
For example if rand normally gives you the following string:
15
62
85
1
Then rand + time might give you this string
20
67
90
6
(same pattern, just +5)
Whereas if you properly seed, you'd get a completely different pattern (sorta -- technically you get the same pattern just at a different point in the sequence so it appears to be different).
Exactly, the pattern will appear to be different, but really it would completely depend on how fast your program ran. Plus you don't want to be checking the time 50 times if you only need to check it once.
Edit: Disch, I think you meant "rand() + time()" not "rand() % time()" as that would be really weird... Hmm...
Thanks for all the replies guys! Sorry, I should have been more specific when I was explaining my problem, it is random every time I run it, close it, and then run it again, but I have a do loop going back to the beginning so you don't have to shut it down for a different outcome. I have what you guys are talking about, I think. Here's my code so you can see it. I know this is most likely very poorly done haha, but here it is.
awesome script. so awesome in fact that i couldnt help but make it myself. i am also a very fresh beginner and so when it says
1>LINK : fatal error LNK1168: cannot open c:\documents and settings\my documents\visual studio 2010\Projects\rockpaperscissors\Debug\rockpaperscissors.exe for writing
i have no idea what to do. can anyone out there in cyberspace help?
C++ isn't a scripting language. Anyways: That either means you currently have the executable running right now, or you generally don't have write permissions for that directory.
haha ok so now im trying to add to your program by specifying whether you win or lose. i would figure this would be a simple addition of a few more if statements like:
I'm pretty sure your problem (or one of them at least) was with while ( a != 1,2,3); //line 44 .
You wanted while( a != 0 || a != 2 || a != 3 ); //or "a < 1 || a > 3" like above .
Are you still having problems with your program?