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
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers.
Thanks (8 Replies)