Random NUmbers Generation with out repetation

Hi
I have the below 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 ?

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

did not work for me. I had to change them to

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.

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

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

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.

Had not much time to look into it so far, but you got some hints I see :slight_smile:

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.

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 :wink:

That does not rule out homework, does it ?

lalitka asks for a perl script only. This makes me suspicious. But the OP gets the benefit of doubt.

It is not clear and we can't proof it. So I guess someone just writes it for him or not :slight_smile: That the example is taken form the Adv. Bash S.G. is nor positive nor negative, just wanted to mention it cause I felt like it.

That he asks for a Perl solution could come of that I wrote in my 2nd post, that there is for sure some handy random function in Perl already:

Since he already might have given up searching for a solution in shell script because no one wrote a solution yet nor he might have found one on the net yet, he just asks for one.
There are people asking for much simpler stuff they could even find with 10 seconds googling, so...
Also there are sometimes much more rude or demanding posts that get answers from the helpful folks so that I personally think if someone has a solution, maybe just post it and it's over.