[Discuss] script or program to generate a list of random numbers

Clarke Brunsdon crimson at uvic.ca
Thu Sep 28 17:28:40 PDT 2006


I'm not exactly sure what you're looking for.
I beleive what you're looking for are N unique random numbers between X
and Y.
If that is correct, the following script will output a unique number per
line.

Save it in a file called uniquerandom.pl (or whatever you wish).

For 5 unique numbers between 0 and 10:
./uniquerandom.pl 5 0 10

#!/usr/bin/perl
use strict;

my $iters = @ARGV[0];
my $min = @ARGV[1];
my $max = @ARGV[2];
my %existing;

for(my $i = 0; $i < $iters; $i++) {
	# +1 to include the max
	my $rand = int(rand ( ( $max + 1) - $min));

	if($existing{$rand}) {
		$i--;
	}
	else {      
		$existing{$rand} = 1;
		print $rand . "\n";
	}
}


if you want it to put it in a file, redirect it with:
./uniquerandom.pl 5 0 10 > myresults


~Clarke

On Thu, 2006-09-28 at 16:52 -0700, Gary Gauthier wrote:
> Hey folks,
> 
> I am not a programmer/developer, but am finding myself in want of something geared to coding.
> 
> What I am trying to accomplish is the following:
> 
> 1. Generate a random number, given a specified range;
> 2. continue to the next generated number;
> 3. check to see if it is a duplicate of a previously generated number;
> 4. if so, delete this instance of the number and regenerate a new one;
> 5. if a delete/regen happens, reset the counter by one;
> 6. repeat until the specified number of iterations has completed;
> 7. output results in a text file
> 
> So besides the fact that I don't code, I also don't know what vehicle would be best suited for this.
> Although, it would be nice to have it be platform independant.
> 
> Do any of the many talented individuals on this list know how to go about accomplishing this?
> 
> I tried to find something online, but was being hampered by the unfamiliarity of what I'm actually looking at when I did find something that looked like a potential solution. Nothing fit the bill exactly and again, I definitely don't know enough to do it myself or modify some existing code.
> 
> Ttyl, Gary
> _______________________________________________
> Discuss mailing list
> Discuss at vlug.org
> http://ladybug.vlug.org/cgi-bin/mailman/listinfo/discuss



More information about the Discuss mailing list