LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2015, 10:14 AM   #1
deelinux
Member
 
Registered: Nov 2009
Posts: 39

Rep: Reputation: 15
Perl rand / srand function


Hello

(New to perl script and programming)

I want to generate 10 random numbers between 1-20, but I only want them listed once in my list.

I have the below script, which works, but I have noticed every now and then, it generates duplicates within the list. I understand it would do this, as its non the wiser (my lack of code), so does anyone know how I would be able to do this in my script or any pointers on how I could only get 10 unique numbers all the time?

I have run both rand and srand - same results


$x=1;
$y=20;
$looptimes=10;

while ($looptimes) {
$mynumbers = int(rand ($y - $x + 1)) + $x, "\n";
print $mynumbers;

$looptimes--;
}


Dee
 
Old 10-16-2015, 11:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, please use [code][/code] tags around code / data.

If you want unique numbers you would need to store all the numbers being generated, probably in an array, so you can check each new number against those already drawn.
 
Old 10-19-2015, 03:38 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you stored them in a hash keyed on the generated value, each entry could only possibly exist once.
 
  


Reply

Tags
perl, scripting



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rand() function in linux ko589 Programming 6 04-28-2009 08:48 AM
looking for a linux audio player that uses the C language rand() function for random dateofexpiration Linux - Software 1 08-14-2007 07:29 PM
ImageMagick: the rand() function and using brackets in commands LinuxLala Linux - Software 0 04-21-2005 01:46 PM
srand(), rand(), RAND_MAX, or XXX weirdness nazdrowie Programming 2 03-07-2005 12:45 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration