Mailing List ArchiveSupport open source code!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] random in bash [Kmail Notification]
- To: tlug@example.com
- Subject: Re: [tlug] random in bash [Kmail Notification]
- From: Mauro Sauco <mauro.sauco@example.com>
- Date: Fri, 8 Mar 2002 09:13:45 +0900
- Content-transfer-encoding: 8bit
- Content-type: text/plain; charset="iso-2022-jp"
- In-reply-to: <20020308084532.3ddd4cc8.s45652001@example.com>
- References: <87d6ygpg4x.fsf@example.com> <Pine.GSO.4.44.0203070936190.793-100000@example.com> <20020308084532.3ddd4cc8.s45652001@example.com>
What about :( from Advanced Bash-Scripting Guide) #!/bin/bash # $RANDOM returns a different random integer at each invocation. # Nominal range: 0 - 32767 (signed 16-bit integer). MAXCOUNT=10 count=1 echo echo "$MAXCOUNT random numbers:" echo "-----------------" while [ "$count" -le $MAXCOUNT ] # Generate 10 ($MAXCOUNT) random integers. do number=$RANDOM echo $number let "count += 1" # Increment count. done echo "-----------------" # If you need a random int within a certain range, use the 'modulo' operator. # This returns the remainder of a division operation. RANGE=500 echo number=$RANDOM let "number %= $RANGE" echo "Random number less than $RANGE --- $number" echo # If you need a random int greater than a lower bound, # then set up a test to discard all numbers below that. FLOOR=200 number=0 #initialize while [ "$number" -le $FLOOR ] do number=$RANDOM done echo "Random number greater than $FLOOR --- $number" echo # May combine above two techniques to retrieve random number between two limits. number=0 #initialize while [ "$number" -le $FLOOR ] do number=$RANDOM let "number %= $RANGE" # Scales $number down within $RANGE. done echo "Random number between $FLOOR and $RANGE --- $number" echo # Generate binary choice, that is, "true" or "false" value. BINARY=2 number=$RANDOM T=1 let "number %= $BINARY" # let "number >>= 14" gives a better random distribution # (right shifts out everything except last binary digit). if [ "$number" -eq $T ] then echo "TRUE" else echo "FALSE" fi echo # May generate toss of the dice. SPOTS=7 # Modulo 7 gives range 0 - 6. DICE=2 ZERO=0 die1=0 die2=0 # Tosses each die separately, and so gives correct odds. while [ "$die1" -eq $ZERO ] # Can't have a zero come up. do let "die1 = $RANDOM % $SPOTS" # Roll first one. done while [ "$die2" -eq $ZERO ] do let "die2 = $RANDOM % $SPOTS" # Roll second one. done let "throw = $die1 + $die2" echo "Throw of the dice = $throw" echo exit 0 On Friday 08 March 2002 08:45 am, Antony Stace wrote: > So would I be correct in concluding that bash cannot produce any random > numbers(or random-ish looking ) by itself without using some other program > or device?
- References:
- Re: [tlug] random in bash [Kmail Notification]
- From: Stephen J. Turnbull
- Re: [tlug] random in bash [Kmail Notification]
- From: Tod McQuillin
- Re: [tlug] random in bash [Kmail Notification]
- From: Antony Stace
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] random in bash [Kmail Notification]
- Next by Date: RE: [tlug] random in bash [Kmail Notification]
- Previous by thread: [tlug] Re: random in bash [Kmail Notification]
- Next by thread: Re: [tlug] Kmail Notification
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links