### Thread: random number

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. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Jul 2001
Location
Oslo
Posts
1,516
Rep Power
18
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?
3. 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
4. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Jul 2001
Location
Oslo
Posts
1,516
Rep Power
18
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.
5. No Profile Picture
WoR
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
6. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2001
Location
Montana
Posts
505
Rep Power
18
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.
7. 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.
8. No Profile Picture
WoR
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.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2002
Location
Oxford, UK
Posts
28
Rep Power
0