|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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. Have Eternal Life PHP Version 5.1.4 MySQL version: 5.0.27-standard |
|
#7
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > random number |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|