A way to store 2 random numbers from a for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A way to store 2 random numbers from a for loop?
# 1  
Old 03-08-2012
A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks.
# 2  
Old 03-08-2012
Code:
# Clear the list of commandline parameters
set --

for X in 1 2
do
        # Add a random number to the end of the parameter list
        set -- "$@" "${RANDOM}"
done

echo "First number was $1"
echo "Second number was $2"

It's also possible to use arrays, but only in certain shells.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-08-2012
Thanks. Is there a way to restrict the random numbers generated from say like 1-100?
# 4  
Old 03-08-2012
Depends. What's your shell? $(((RANDOM%100)+1)) should work instead of ${RANDOM} for bash or ksh.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-08-2012
I'm using Korn, which worked perfectly. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Programming

How to fill a memory with random numbers in C

Hi, I have a parametrized memory mem. I want to fill this memory with random numbers with respect to the data_width. can anyone help me on this.. (3 Replies)
Discussion started by: vdhingra123
3 Replies

3. Shell Programming and Scripting

Loop assistance, getting array of random numbers and feeding to a command, how-to?

Hi all, I need a little assistance to complete the following script. I would like to take a file with a single number on each line and for each number, run it through a command. The loop will terminate once all numbers have been checked. Here is what I have thus far... COUNTER=`wc -l... (2 Replies)
Discussion started by: boolean2222
2 Replies

4. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

5. 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

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. Programming

How to set constrain on random numbers in c

Hi, I am currently trying to generate multiple random numbers in C for different variable:- die1=1+(rand()%5); die2=1+(rand()%5); die3=1+(rand()%5); die4=1+(rand()%5); But I need to contrain the total of die1, die2,die3 and die4 to be 5 as well. If i insert die1+die2+die3+die4=5, i do... (6 Replies)
Discussion started by: ahjiefreak
6 Replies

8. Shell Programming and Scripting

genrating pseudo random numbers

I want to generate the file in following format -------------------------------------- mov t1, %r1 mov t2, %g1 mov t3, %o1 . . . . m times add %r1, %g1, %o1 add %r2, %g2, %o2 . . . n times ------------------------------------------- (7 Replies)
Discussion started by: hack_tom
7 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