Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Random numbers in functions




Thanks! But since I'm planning to obtain many random numbers I'd like to do this without file operations. Using a global variable should be equivalent to your solution but the output of rand48() seems to be convergent.

Is the seed or the state of the generator saved?

Here's a simplified version of what I'm looking at:

The lib (functions.zsh):

typeset -g RANDOM_PARAMETER="abcabcabcabc"

random()
{
  print "RP: $RANDOM_PARAMETER"
  T=$(( rand48(RANDOM_PARAMETER) * 1000 ))
  print "RP: $RANDOM_PARAMETER"

  RESULT=$(( T % 10 ))
  print "RESULT: ${RESULT}"

  print
}

The call:

source functions.zsh
for i in {1..4} ; { print "TOP_RP: $RANDOM_PARAMETER" ; random }

The result:

TOP_RP: abcabcabcabc
RP: abcabcabcabc
RP: 004db541003a
RESULT: 0

TOP_RP: 004db541003a
RP: 004db541003a
RP: 004db541003a
RESULT: 0

TOP_RP: 004db541003a
RP: 004db541003a
RP: 004db541003a
RESULT: 0

TOP_RP: 004db541003a
RP: 004db541003a
RP: 004db541003a
RESULT: 0


--
Justin Michael Joseph Wozniak

On Tue, 20 Nov 2007, Peter Stephenson wrote:



Messages sorted by: Reverse Date, Date, Thread, Author