| Aceix (476) | |||
Why does this give me the same number every time? ie: 3
Aceix. | |||
|
Last edited on
|
|||
| Chervil (1203) | |
|
Because you use the same seed each time. | |
|
|
|
| Aceix (476) | |
|
Never mind. Problem SOLVED!!! Soln: srand(time(NULL));Thanks for trying to help, Aceix. | |
|
|
|
| Disch (8615) | |
|
Note you should NOT call srand in this function at all. You should call srand() exactly once when the program starts. You must not call it every time you generate a new random number. | |
|
|
|
| Rayquaza4096 (2) | |
| Disch, why do you advise against calling srand() whenever you generate a random number? I am guilty of doing this in many of my programs, and I am curious as to why it is considered bad practice. | |
|
|
|
| EssGeEich (1007) | |
|
1. Because it's a waste of performance. 2. Because it's not needed. 3. Because it's just a risk of non-randomness in certain cases. So: Just stick with what Disch said, Just call srand() once, unless you need to reproduce a bug (eg if a bug is rand-dependent, in Debug mode you may want to call srand ~once~ with a specific number so you can reproduce and debug the bug) or you don't need complete randomness. | |
|
|
|
| Rayquaza4096 (2) | |
| Thanks for your help! It makes sense that it is not needed to call srand() too many times. | |
|
|
|