Random NUmbers Generation with out repetation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random NUmbers Generation with out repetation
# 1  
Old 11-25-2008
Random NUmbers Generation with out repetation

Hi
I have the below code

Code:
 
MAXCOUNT=10
count=1
echo
echo "$MAXCOUNT random numbers:"
echo "-----------------"
while [ "$count" -le $MAXCOUNT ]      # Generate 10 ($MAXCOUNT) random integers.
do
 number=$[ [$RANDOM % 20  ] + 1 ]
    "echo $number"
  let "count += 1"  # Increment count.
done

But aftre executing this

i am getting repetative randiom numbers like ,2,5,6,6,10,8,18,14

but i want unique random numbers ?

can any one help me in solving this ?

Last edited by zaxxon; 11-25-2008 at 02:15 AM.. Reason: codetags implemented
# 2  
Old 11-25-2008
Pipe the numbers into sort & uniq. You will get than less since the duplicates will be deleted, but maybe you have another loop checking the count of numbers and generate some more. Maybe there is already some nice random function in Perl.

Also these lines
Quote:
number=$[ [$RANDOM % 20 ] + 1 ]
"echo $number"
did not work for me. I had to change them to
Code:
number=$(( ( $RANDOM % 20 ) + 1 ))
echo "$number"

The 1st line could be because of your shell, but the " in front of the echo seems to be a typo.

Also please use [ code ] and [ /code ] tags to make your code better visible; I edited your post to show what I mean, look for the blue background of your code.

Last edited by zaxxon; 11-25-2008 at 02:24 AM..
# 3  
Old 11-25-2008
Random Numbers generation with out repeattion

hey thanks for ur response, can u explain me clearly what i have to do now i mean .how i hav eto pipe and sort , i am very new to shells cripting thats why i amasking u the code for that

and i wnat to generate 1 to 20 random unique numbers
# 4  
Old 11-25-2008
Generate Random numbers from 1 to 18 using perl script

Hi

Can any one give me a perl script for generating random numbers from 1 to 18 only , and the random numbers should be unique .................

please help me in solving this (only perl Script
# 5  
Old 11-25-2008
Are you posting homework assignments? Why do you need different random numbers from 1 to 18?

There are 2 ways:

1/ keep generating random numbers, reject the ones you already have (use a hash to store them) until you have enough different ones
-> This creates a lot of extra work, and if you want more random numbers than available in your range, you'll get an infinite loop. Worst-case execution time is unpredictable (averages can be calculated with use of statistics)

2/ Generate an array of "accepted values" and pick a random element from the array, then remove it from the array.
-> Nice & fast if you got a small range, predictable worst-case scenario

If you want me to write out the script, take a minute to explain why you need it. If it's for homework, we won't solve it for you. We'll help you think and we'll look at your scripts and find mistakes, but won't write the whole thing for you.
# 6  
Old 11-25-2008
Had not much time to look into it so far, but you got some hints I see Smilie

Must not be homework; it could be out of interesst or something like that. The sample code lalitka posted I found in the Advanced Bash Scripting Guide.
# 7  
Old 11-25-2008
I'll even write it in perl for him if he can convince me it's not homework. But you learn a lot more from trying yourself (and having your mistakes corrected) than reading other peoples code.

There's lots of "other peoples code" on the web, too Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Random password generation

Hello, I have created a script to generate a random password on Linux/Solaris, but I simply cannot use it on my AIX VMs since Bash isn't installed on them. I need a password that is randomly created with the following... (12 Replies)
Discussion started by: gfroute
12 Replies

2. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

3. Shell Programming and Scripting

Random word generation with AWK

Hi - I have a word GTTCAGAGTTCTACAGTCCGACGAT I need to extract all the possible "chunks" of 7 or above letter "words" from this. SO, my out put should be GTTCAGA TTCAGAG TCAGAGT CAGAGTTCT TCCGACGAT CAGTCCGACG etc. How can I do that with awk or any other language? I have no... (2 Replies)
Discussion started by: polsum
2 Replies

4. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

5. Shell Programming and Scripting

random number generation in ksh

i tried to use $random function in unix i simply typed print $random at shell and it returnted no value is there any function in korn shell that i can use to generate random number help is appreciated (2 Replies)
Discussion started by: er_zeeshan05
2 Replies

6. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

7. UNIX for Dummies Questions & Answers

Random number generation in ksh

I need to generate a random number in ksh everytime I run the script ,the range should be from 100 to 24800,I could use $RANDOM but I seem to have no control over the range of numbers ,could you please suggest some way I could implement this .Thanks. Mervin (2 Replies)
Discussion started by: mervin2006
2 Replies

8. Programming

Random number generation

Hi...How can I generate random numbers between a given range ...preferably between 1 and 100...in UNIX C programming...? I tried out functions like rand(),drand48() etc but didnt get satisfactory results... Thanks a lot in advance.......... (1 Reply)
Discussion started by: tej.buch
1 Replies

9. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question