Solved

Generating unique random numbers

Posted on 1998-01-31
4
1,774 Views
Last Modified: 2008-02-01
The following code will generate nine random numbers, but how can I change the code so that it will generate nine unique random numbers?

srand (time|$$);
$square=1;
while ($square != 10) {
$random=int(rand(50))+1;
$word{$square}=$random;
$square++
}
0
Comment
Question by:bazzle
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
icd earned 400 total points
ID: 1209713
First of all any computer program cannot generate true random numbers, only pseudo random. That is they are deterministic if you know the algorithm but they have certain properties of truly random numbers.

To solve your problem, there are two ways.

1. You keep a record of all the previous random numbers and compare each new number against the list. This is a fairly trivial approach but it might not be practical for very large sequencies of numbers and it will get slower as you generate more and more numbers. If this is OK then I will post a solution for you.

2. Use another random number generator that has the properties you want. I will post this answer in a following comment (when I have had time to look it up!)

0
 
LVL 5

Expert Comment

by:icd
ID: 1209714
OK. Here is an alternative that will always give a pseudo random number sequence, without repeats (upto a maximum of 50 numbers!).

$random = (time|$$);
$square=1;
while ($square != 10) {
$random = ($random * 213 + 11) % 50;
$word{$square}=$random;
$square++
}

The sequence will give you ten numbers, guaranteed to be different and showing pseudo random characteristics. Given the same seed value the generator will always generate the same sequence, but that  is true of any computed random number sequence.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1209715
(time+$$) is more likely to give a unique seed than (time|$$)

 $random = ($random * 213 + 11) % 50;
Might as well be
 $random = ($random * 13 + 11) % 50;
And there's correlation in the way different batches of 10 overlap each other.
It's also weak in that there are only 50 distinct seeds.

One way to implement suggestion 1. could be:
 1 while( $previous{$random=int(rand(50))+1} )
 $previous{$random}=1;

But for large sequences, the approach from the FAQ may be better:
 srand;
 @new = ();
 @old = 1 .. 50;
 for( @old ){
   my $r = rand @new+1;
   push(@new,$new[$r]);
   $new[$r] = $_;
 }
#or
 @word=();
 @old = 1 .. 50;
 while( @word < 10 ){
  my $r = rand @old;
  push(@word,$old[$r]);
  $old[$r] = $old[-1];
  pop(@old);
 }
(since you're indexing %word with integers in sequence,
you might as well use an @word array instead of a hash)

0
 
LVL 84

Expert Comment

by:ozo
ID: 1209716
Another problem with
 $random = ($random * 213 + 11) % 50;
is that with a seed of 22 (or 47) it generates only 2 unique values.
(there are also 8 seeds which generate only 4 unique values)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 128
pattern matching in perl 2 103
Check file date before getting folder path 1 72
Excel to CSV conversion with specific columns 5 80
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now