| brandonator (110) | |||
i was wondering if there was a better way to generate a random number than
because if you ask for two random numbers at the same second they will come up with the exact same number. | |||
|
|
|||
| Peter87 (3691) | |
| Only call srand once in your program, at the beginning of main is a good place, and you will not have this problem. | |
|
|
|
| whitenite1 (705) | |||
|
@brandonator Try it this way..
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. | |||
|
|
|||
| brandonator (110) | |||||
|
i know how to do random numbers but they will come up with the same number during the same second because its based on the second of time example:
note that something like this will come up with the same number each time, during the same second would print something like
| |||||
|
Last edited on
|
|||||
| Moschops (5961) | |
|
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.
| |
|
Last edited on
|
|
| whitenite1 (705) | |
|
@brandonator 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.
| |
|
|
|
| brandonator (110) | |
| edited my post, please read, would your way fix this? | |
|
|
|
| whitenite1 (705) | |||
fixed
| |||
|
Last edited on
|
|||
| brandonator (110) | |
@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 123123123123123123123123123 ect.
| |
|
Last edited on
|
|
| Moschops (5961) | ||
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. | ||
|
Last edited on
|
||
| brandonator (110) | |||||
|
yeah im wondering how to do that, btw i want the loop to continue going in the example to see the results can you show me a better way to do random functions than these two examples
or
so it will actually come up with random (or more random results) such as 324596236423542934562356645235432542354325429423856243423where theres not a predictable patturn but is chance for repitition with the numbers 1-3 | |||||
|
Last edited on
|
|||||
| whitenite1 (705) | |
|
@brandonator Please post the code you have now, that prints out the series 12312312312312312312312312312312312312312312312312312312312312Also, what compiler are you using? I use MS Visual C++ 2010 Express | |
|
|
|
| brandonator (110) | ||
note the post
like in the middle it did 235664523 6 repeatedand 324596236423542934562356645235432542354325429423856243423is not very predictable (typed random numbers)and answer to question dev c++ | ||
|
Last edited on
|
||
| whitenite1 (705) | |||
|
@brandonator 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.
| |||
|
|
|||
| brandonator (110) | |
| 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) | |
|
|
|
| Disch (8348) | ||||
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:
| ||||
|
|
||||
| brandonator (110) | |||
yeah that was what was confusing me, i was trying to understand why
was working when srand( (unsigned) time(0) );was not, thats why the understandability of his program was confusing (when it was my mistake and those two made no difference) | |||
|
|
|||
| Disch (8348) | |
| 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. | |
|
|
|
| whitenite1 (705) | |
|
@Disch t holding the time makes ALL the difference, since it's the time that is seeding srand. | |
|
|
|
| helios (10126) | |||
It makes no difference. The semantics of time() is
| |||
|
|
|||