Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 25th, 2002, 07:48 PM
getchoo getchoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 20 getchoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old March 26th, 2002, 02:11 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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

Reply With Quote
  #3  
Old March 26th, 2002, 07:30 AM
getchoo getchoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 20 getchoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #4  
Old March 26th, 2002, 01:03 PM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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.

Reply With Quote
  #5  
Old March 27th, 2002, 03:45 AM
WoR WoR is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 23 WoR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #6  
Old April 12th, 2002, 09:39 AM
Mark_php Mark_php is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Montana
Posts: 426 Mark_php User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 14 h 34 m 38 sec
Reputation Power: 8
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

Reply With Quote
  #7  
Old April 12th, 2002, 10:39 AM
The Ostrich The Ostrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Oxford, UK
Posts: 31 The Ostrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 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.

Reply With Quote
  #8  
Old April 23rd, 2002, 06:55 AM
WoR WoR is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 23 WoR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #9  
Old April 26th, 2002, 07:42 PM
The Ostrich The Ostrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Oxford, UK
Posts: 31 The Ostrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > random number


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT