$RANDOM question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers $RANDOM question
# 1  
Old 05-05-2011
$RANDOM question

I want to generate a random number between 1 and 22.

I believe that the variable $RANDOM generates numbers in the inverval 1-32767.

How do I generate a random integer within a specific interval?
# 2  
Old 05-05-2011
Code:
$(($RANDOM % 22 + 1))

# 3  
Old 05-05-2011
The rules for $RANDOM are in the "man" page for your Shell.
It's usually 0-32767 (not 1-32767).

One idea.
Add 22 to $RANDOM.
Divide by 22 and take the remainder. Add one to that result.
Final result should be in the range 1-22 .

The method for the mathematics in Shell depends on which Shell you are using. Modern Posix Shells include easy ways to divide and return the remainder.


Just read "purdeym" post, which is using Posix Shell arithmetic. Just needs to add 22 to $RANDOM to allow for values of $RANDOM which are less than 22.
# 4  
Old 05-05-2011
I think that code works perfectly...

Code:
>echo $((0 % 22 + 1))
1
>echo $((1 % 22 + 1))
2
>echo $((2 % 22 + 1))
3
>echo $((3 % 22 + 1))
4
>echo $((4 % 22 + 1))
5
>echo $((5 % 22 + 1))
6
>echo $((21 % 22 + 1))
22

>echo $((22 % 22 + 1))
1
>echo $((23 % 22 + 1))
2
>echo $((24 % 22 + 1))
3

But if not, let me know.
# 5  
Old 05-05-2011
Quote:
Originally Posted by purdym
I think that code works perfectly...
So do I.
# 6  
Old 05-09-2011
So do I . Albeit a bit after the fact.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

2. Shell Programming and Scripting

Random ordering

1 2 4 5 3 I would like to use a script so that i can randomly rearrange these numbers such as 3 5 2 4 1 Thanks! (3 Replies)
Discussion started by: johnkim0806
3 Replies

3. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

4. UNIX for Dummies Questions & Answers

random words

Hi there folks, for an exercise for my pupils (you know i am always thinking of them!) i need to randomly re-arrange the words (blank space separated) in a sentence (a line in a textfile). Any inspiration?? Txk so much. (9 Replies)
Discussion started by: eldeingles
9 Replies

5. UNIX for Dummies Questions & Answers

Random fields

Hi, there folks! Well, trying again to build exercises for my students I come across the following idea. Inputfile: csv with two fields (f1 and f2) separated by a tab. Each field contains half a sentence, which added to the one contained in the other field forms a complete sentence (s), so... (2 Replies)
Discussion started by: eldeingles
2 Replies

6. Windows & DOS: Issues & Discussions

random RAM question

Random question. 32 bit windows can't address more the 4 gb of RAM, right. So if you have 4gb of “normal” RAM and 1 gb graphics card window only find 3gb of “normal” ram + your graphics card following on from this If you where to put 2 graphics card with 2gb of ram on them each,... (4 Replies)
Discussion started by: THM
4 Replies

7. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

8. Shell Programming and Scripting

Random command

I am trying to select one random word from a file, any ideas on how to do this as i have only manged to generete the random number? (1 Reply)
Discussion started by: melaz
1 Replies

9. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies

10. AIX

random in ksh

hello I must to create a ksh script to give a random password with letter, number, and 8 digits. I can't use the random command with ksh, there is a similar command ? thank you (1 Reply)
Discussion started by: pascalbout
1 Replies
Login or Register to Ask a Question