The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to set constrain on random numbers in c ahjiefreak High Level Programming 6 01-11-2008 04:46 AM
how can i isolate the random sequence of numbers using awk? rcon1 UNIX for Dummies Questions & Answers 2 01-09-2008 09:06 AM
generating random numbers with hamming distance 4 hack_tom Shell Programming and Scripting 2 04-29-2007 01:27 AM
genrating pseudo random numbers hack_tom Shell Programming and Scripting 7 04-27-2007 10:10 PM
Sed - Replacing numbers w/asteriks in random location in a file giannicello UNIX for Dummies Questions & Answers 9 10-04-2001 12:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2005
asal_email asal_email is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 9
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
  #2 (permalink)  
Old 07-13-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
awk -v loop=10 -v range=100 'BEGIN{
  srand()
  do {
    numb = 1 + int(rand() * range)
    if (!(numb in prev)) {
       print numb
       prev[numb] = 1
       count++
    }
  } while (count<loop)
}'
  #3 (permalink)  
Old 07-13-2005
asal_email asal_email is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 9
Thanks, great code !!! but I need some clarifications.

when you compare the generated number to previous one, using :
if (!(numb in prev))
isn't comparing only the last generated random number with the current one or comparing all previous generated random number ? so only uniq number retained.

Thanks
  #4 (permalink)  
Old 07-13-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
The code compares all previously generated numbers which are stored in an array called 'prev'.
  #5 (permalink)  
Old 07-14-2005
r2007's Avatar
r2007 r2007 is offline
Registered User
  
 

Join Date: Jun 2005
Location: China
Posts: 73
Select 10 numbers from 1 through 80

Code:
for (( i=1;i<=80;i++ )) do echo $RANDOM $i; done|sort -k1|cut
 -d" " -f2|head -10
it works using bash
  #6 (permalink)  
Old 07-14-2005
asal_email asal_email is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 9
I actually want to use those random number on the fly such :
#! /bin/bash
while (condition)
do
number=$RANDOM
do some work here using random number until condition satisfied
done

the condition is independent of generated random number
but random numbers have to be generated as along as condition is met and
all numbers generated have to be unique.

Thanks again
  #7 (permalink)  
Old 07-14-2005
r2007's Avatar
r2007 r2007 is offline
Registered User
  
 

Join Date: Jun 2005
Location: China
Posts: 73
Quote:
Originally Posted by asal_email
I actually want to use those random number on the fly such :
#! /bin/bash
while (condition)
do
number=$RANDOM
do some work here using random number until condition satisfied
done

the condition is independent of generated random number
but random numbers have to be generated as along as condition is met and
all numbers generated have to be unique.

Thanks again
well, the bash variable "$RANROM" is a random integer between 0 and 32767. if the condition is independent of generated random number, try to imagine the loop block has already run 32768 cycles, all of numbers used up.
so the condition must consider the random numbers rang.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0