If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Generating random numbers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-20-04, 09:02
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Question Generating random numbers

Hi everybody.

I hope you can shed some light here.
How can I generate random numbers inside one do-loop ?

Here is what I have so far:

# Random number
for (( i=1; i<=500; i++)
do
j = random
printf $j
end

The problem with this loop is that running this script twice,
the same 500 'random' numbers are generated.
I thought about using the date in seconds as a seed but
I am not sure whether this is the most eficient way to do it.
I appreciate any help

Thanks,

Serg
Reply With Quote
  #2 (permalink)  
Old 02-20-04, 11:08
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Use $SECONDS as seed
Code:
RANDOM=$SECONDS
for ( i=1; i<=500; i++))
do
   j = random
   printf $j
end
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-20-04, 13:14
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
You can use the RANDOM environment variable to generate random numbers.

echo ${RANDOM}

If you wanted a number between say 1 and 500, you would use...

echo $((RANDOM % 500 +1))
Reply With Quote
  #4 (permalink)  
Old 02-20-04, 20:06
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Exclamation

Thanks all.

I tried to use $SECONDS but...this is my short script :

random=$seconds
for (( i=1; i<= 500; i++))
do
j=random
printf "$i number is $j";
end

and the output is

1 number is random
2 number is random
...
500 number is random

Could be a typo ?

Thanks again, Serg.
Obs. I will check your suggestion too, Damien.
Reply With Quote
  #5 (permalink)  
Old 02-21-04, 05:42
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
typo error for me and for you
Unix is case sensitive ...
Code:
RANDOM=$SECONDS
for (( i=1; i<= 500; i++))
do
   j=$RANDOM
   printf "$i number is $j";
done
__________________
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 02-21-04, 10:45
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Cool

Thanks, Jean Pierre.

It was indeed those typos.
Anyway, the generated random numbers are
not really random because running the script
twice create the same set of 500 random numbers.

I am using bash shell. Do you
know if bash recognize comand $seconds ?
I solved this problem using

random=$(date +%s)

Thanks again for your help.

Serg
Reply With Quote
  #7 (permalink)  
Old 02-21-04, 11:18
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Extracted from 'bash' man page :
Quote:
`SECONDS'
This variable expands to the number of seconds since the shell was
started. Assignment to this variable resets the count to the
value assigned, and the expanded value becomes the value assigned
plus the number of seconds since the assignment.
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On