Thread: random number

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    random number


    Can someone pls provide me with an algorithim that generates an random number and it cannot generate the same random number more than once.

    Thanx

    getchoo
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2001
    Location
    Oslo
    Posts
    1,516
    Rep Power
    15
    I hope you're good at math... http://www.math.keio.ac.jp/~matumoto/emt.html

    I don't think this generator will avoid generating the same number twice, but then again, I don't think any available (pseudo)random number generators can do that.

    Any particular reason why you want to implement this yourself when there are so many algorithms available?
    --
    Regards
    André Nęss

    Puritanism: The haunting fear that someone, somewhere may be having fun
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0
    The reason, is that I'm creating a form that generates a random student number for a new student, but I realize that I could instead just use a certain number as a starting point and just increment it by one for each new student.

    Thanx for you help

    gechoo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2001
    Location
    Oslo
    Posts
    1,516
    Rep Power
    15
    Eh... any programming language either has support for random numbers built-in or through a library, anyway, the incrementing method is much better for this purpose.
    --
    Regards
    André Nęss

    Puritanism: The haunting fear that someone, somewhere may be having fun
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    20
    Rep Power
    0
    while there are some generators of the form
    (a*x+b mod 2^n-1)
    which take a long way to loop, they are NOT good generators.
    The standard (easy) way to get good non repeating random numbers, is to create a sequence of numbers [0..N] in a list or array and the do a series of radom exchanges on the sequence.
    I believe about NlogN exchanges should do (thats hte min amount it takes to sort szch numbers).
    The you are absolutely sure you have N non repeating numbers.

    If you are unsure of how many iterations to do, try this.
    Attach a random number to each number of the sequence, and then sort the random numbers. Even if these random numbers are not unique, your attached sequence will still be so.

    Hope this solves the problem

    WoR
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Montana
    Posts
    504
    Rep Power
    14
    Could you take the day, hour, month and year and then tack on a rand() number on the end. Since the day, hour month and year only come once it would be unique.
    Have Eternal Life
    Learning is so unproductive...
    The more I learn the more questions I have!
    Therefore I am going backwards.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Oxford, UK
    Posts
    28
    Rep Power
    0
    Okay... Even if getchoo uses a PRNG which loops without repeating elements, he will not be able to get numbers in a range less than the maximum range of the PRNG with this property, which is a bit useless. I would have said the best way to do this would be to store a hash table of all the numbers that have been generated so far, and just to multiple random probes until you find an empty slot.

    The Mersenne Twister does not avoid generating the same number twice. In fact, it passes statistical tests designed to detect these occurences up to about 10^11 32-bit elements.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    20
    Rep Power
    0
    I guess I hadn't read getchoos last mail well enough.
    I like Marks idea.
    When I write quotes or bills I generate a number similarily:
    printf("%4d, %2d, %3d", Year, Month, IndexStartingAt0EachMonth);
    This is unique and hte number even holds some info which is good for searching.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Oxford, UK
    Posts
    28
    Rep Power
    0
    I have a new idea about this. You could...

    Generate a new number for each student (in order).
    Use the number as input to the SHA-1 algorithm.
    Get out 160 bit output, and use that as a unique identifier for the student.

IMN logo majestic logo threadwatch logo seochat tools logo