C: First value from rand() is zero (or other problem?)
John Kauffman
Posts: 653
I've written below as an experiment. I always get zero as first output from rand(). Or maybe problem is elsewhere. Any suggestions? Thanks.
#include "simpletools.h"
int main(void){
int randomCurrent;
print("Count\tValues\tModuli range 0-5\n");
for(int count=1;count<=100;count++)
{
randomCurrent = rand();
print("%d.\t%d\t%d\n",count,randomCurrent,randomCurrent%6);
}
Terminal output always shows 0 for first entry:
Count Values Moduli range 0-5
1. 0 0
2. 21468 0
3. 9988 4
4. 22117 1
5. 3498 0
#include "simpletools.h"
int main(void){
int randomCurrent;
print("Count\tValues\tModuli range 0-5\n");
for(int count=1;count<=100;count++)
{
randomCurrent = rand();
print("%d.\t%d\t%d\n",count,randomCurrent,randomCurrent%6);
}
Terminal output always shows 0 for first entry:
Count Values Moduli range 0-5
1. 0 0
2. 21468 0
3. 9988 4
4. 22117 1
5. 3498 0
Comments
If you have a real time clock or similar input that will vary each time the program runs, you can use a value from this as the input to srand() to ensure that rand() produces a different sequence each time.
I don't have RTC. Do you know of a way to get a seed from Prop ticks?
Much thanks.
http://obex.parallax.com/object/498
Should be relatively straightforward to implement that in C.
Depends on how random you want.