|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Random numbers every millisecond?
The following code creates a random number by "srand"ing with a value from "time" which only changes every second. This means the random numbers are the same if the program is run twice during the same second. How can I change this to be MORE random more often (every millisecond or smaller)?
Quote:
|
|
#2
|
|||
|
|||
|
Matthew,
If you are using a UNIX system, you can get your randomness from one of the random devices that most systems have now. If you need to generate a string of random numbers for your application, you would be better served to write your program for such a purpose. For example: Code:
int count;
int i;
srand(time(0));
printf("How many numbers? ");
sscanf("%d", &count);
for(i=0; i < count; i++) {
printf("%d", rand() % 100);
if (i % 5) printf("\t");
else printf("\n");
}
__________________
Clay Dowling Lazarus Notes Articles and commentary on web development http://www.lazarusid.com/notes/ |
|
#3
|
|||
|
|||
|
To explain ClayD's code a bit more.
rand() is realy just a list of numbers. srand() determines where in the list to start. In your example srand() is called every time you want a random number. This resets the 'list' to the same position if the time has not changed ie called in same second. ClayD's will only call it once so the list will be incremented after each call to rand() . Note: rand() is not random. It has an bias of around ten percent. If ClayD's solution is not random enough then run a search on google for more random number generators.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions. Frank Zappa |
|
#4
|
|||
|
|||
|
I understand how rand() and srand() work. I just need a value that changes more often than every second to pass into srand(). If I had one that changed every 1000th of a second, that would do it.
|
|
#5
|
|||
|
|||
|
if you used srand for every number then you would need a clock that changed very quickly, but cd's code doesn't call srand for every number.
cd's code only srands once. then all of the numbers are generated onwards from that first srand number position. that's how i see it anyway, so you don't need a quick changing clock. srand and seeding - same thing right? |
|
#6
|
|||
|
|||
|
I do not want to use srand for each number, just for each execute of my script, in which the possibility exists that it can be run twice within one second.
srand uses a seed to start a random sequence, so I think srand and seeding is the same thing. |
|
#7
|
|||
|
|||
|
you didn't make that clear that you wanted more than one lot of random numbers - that that's what the problem was.
it's still the same issue though - you just make sure you only seed the number once. that's what the problem is here - seeding at the same time, or not. so put this round the srand call within your random function to make sure srand gets called only once: Code:
static int x=0;
if(x==0) {
srand(time(0));
x=1;
}
static sustains the x value, in whichever function or block that it is used. so it'll stay at 1 even when the programme leaves that block. |
|
#8
|
||||
|
||||
|
Since you're using *nix, why don't you try reading from /dev/random or /dev/urandom. Something like this:
Code:
#include <stdio.h>
int main(void) {
FILE *fp;
unsigned char c1;
if ((fp = fopen("/dev/urandom", "r")) == NULL) {
fprintf(stderr, "Error! Could not open /dev/urandom for read\n");
return -1;
}
c1 = fgetc(fp);
fclose(fp);
printf("%d\n", c1);
return 0;
}
Note that the code above reads a single char at a time, so the rand value will be between 0 and 255. If you want larger values, read more bytes from it! |
|
#9
|
|||
|
|||
|
You could try GetTickCount() for a millisec timer (with no accuracy)
|
|
#10
|
|||
|
|||
|
Quote:
In my first post I said, "This means the random numbers are the same if the program is run twice during the same second." So I thought that covered it. Sorry for being unclear! |
|
#11
|
|||
|
|||
|
balance,
I'm not sure if you still understand what I meant. I only want to use srand once during the program. But I want the seed passed into srand to be different every time, even if the program is run twice within the same second. |
|
#12
|
|||
|
|||
|
Quote:
Cool. What is /dev/random anyway? How is it filled? |
|
#13
|
|||
|
|||
|
Quote:
What unit defines GetTickCount()? |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Random numbers every millisecond? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|