#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12

    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)?

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    void main()
    {
    unsigned int number;
    time_t t;

    srand((unsigned)time(&t));
    number = rand() % 100;
    printf("%d", number);
    }
    Matthew Doucette / Xona.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    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/
  4. #3
  5. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    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
  6. #4
  7. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    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.
    Matthew Doucette / Xona.com
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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?
  10. #6
  11. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    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.
    Matthew Doucette / Xona.com
  12. #7
  13. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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.
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    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!
  16. #9
  17. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    You could try GetTickCount() for a millisec timer (with no accuracy)
    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
  18. #10
  19. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    Originally posted by balance
    you didn't make that clear that you wanted more than one lot of random numbers - that that's what the problem was.
    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!
    Matthew Doucette / Xona.com
  20. #11
  21. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    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.
    Matthew Doucette / Xona.com
  22. #12
  23. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    Originally posted by Scorpions4ever
    Since you're using *nix, why don't you try reading from /dev/random or /dev/urandom.
    Cool. What is /dev/random anyway? How is it filled?
    Matthew Doucette / Xona.com
  24. #13
  25. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    12
    Originally posted by TechNoFear
    You could try GetTickCount() for a millisec timer (with no accuracy)
    What unit defines GetTickCount()?
    Matthew Doucette / Xona.com

IMN logo majestic logo threadwatch logo seochat tools logo