![]() |
rand() isn't so random
I'm having a problem with the rand() function in the C standard library. I'm sure I'm not the first to complain about this. Every time I use this function, it generates a seemingly random number. But the number(s) is the same each time the program is ran. Is there a way around this? Or a better random number generator somewhere, that doesn't use seeds to generate random numbers? (like a generator based on the time and date)
|
Take a look at this.
|
You need to call srand and give it a seed value which you can get from the time function
:
#include <stdlib.h> |
I thought someone would bring this up. I've tried this. But I still get the same results each time the program is run. I read an article about rand() saying I should seed it with the time. It still yields the same results...
|
Maybe post some code so we can have a look.
|
Well i tested my own code, and it definatly gives different results each time i run it.
|
Like even this for example:
:
#include <stdio.h>Keeps giving me: 25 10 33 16 45 |
Well i compiled you code and got 31 0 13 28 39 on the first run and 29 42 13 8 35 on the second run. So i guess it something up with your compiler, but i have no idea.
|
I got 29 32 20 20 29, then 5 32 23 28 30, so it's randomises correctly for me as well. I'm using GCC.
|
Zap -
what C compiler and OS? |
| All times are GMT -5. The time now is 7:59 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC