I don't see how the two random numbers would be the same, though. Could you show the code that gives them? It would be helpful in determining the cause.
The random number generator rand isn't random. It just reads from a sequence. If the sequence starts with the same seed each time, it'll be the same sequence. You set the seed with srand.
If you set the seed the same way each time, with srand(time(0));, which is the same every time, you'll get the same sequence each time. Use srand ONCE.
That's because you're reseeding the random number generator by using the srand(time(0)) in the do loop. Move it outside the loop, then cout two or more random numbers. You should find they are different each time.
@whitenight1 you did a slight error line 21 ( x != 50); should be ( x = 50); to keep the loop going, also can you explain what you did with the code, because now its a little better, still very predictable but better
you did a slight error line 21 ( x != 50); should be ( x = 50); to keep the loop going
Doing that would make the loop run forever. If that's what you wanted, you'd just have a while(1) at the top and be done with it.
If your implementation gives 123123123123123123123123123 then it's really, really bad.
The rand that typically comes with the compiler is bad. Really, really bad. If you want decent random numbers use the C++11 random number functions, or a decent random number generator from Boost or some other library.
Please post the code you have now, that prints out the series 12312312312312312312312312312312312312312312312312312312312312
Also, what compiler are you using? I use MS Visual C++ 2010 Express
I took your code, added a few small additions for readability, and compiled with Dev-C++. As you can see, it will print out 360 random numbers which should give you an idea of its randomness. Of course, there will always be the chance for repetition, especially with only having 3 random numbers to choose from.
sorry read your code and becausse of the speed i thought it was 123123123, when in reality it was more random (put a pause at the end), anyways, can you explain why your way comes up with better random numbers (line by line)
can you explain why your way comes up with better random numbers
Because he only calls srand once, at the start of his program. He does not call it for every random number like you were.
Only other thing I have to contribute is here:
1 2 3 4 5 6 7
// this is a little silly:
time_t t;
srand((unsigned) time(&t));
// there's no point is creating and filling 't' if you're not going to use it
// just do this instead:
srand( (unsigned) time(0) );
Yes the only difference between those two pieces of code is that 't' holds the current time. Since you don't care about the time, you don't need 't'. There is zero difference as far as srand/rand goes.