• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Random number generator -- OS X different
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random number generator -- OS X different


  • Subject: Re: Random number generator -- OS X different
  • From: Bill Briggs <email@hidden>
  • Date: Sat, 21 Apr 2001 14:25:25 -0300

At 12:02 AM -0400 21/04/01, Daniel Robinson wrote:
It was brought to my attention by Bill Fancher that when run on OS X,
Script A, posted yesterday, gave skewed results.

After stepping through, I find that the error occurred when OS X rounded
the results to a an integer. In Script B, adding 0.5 to the result
solves the problem.

I don't think that's what's going on. Take a look at the two scripts very closely.

In Script A you generate at random an integer in the range from 0 to 9 inclusive. OS X doesn't do any rounding because you've already specified the upper bounds for the random number generator, and since the default lower bound is zero, it returns an integer, not a real (see syntax below). No "rounding" happens in this script, as it does in script B when you perform the coercion to an integer. If you expected a real, then you have to leave the command without an argument. Even if you specify the arguments as 0 and 1, you still get an integer, not a real as a result.

random number from 0 to 1 -- returns integer, 0 or 1

random number -- returns real between 0 and 1

To finish the script, it increments by 1 to get it in the range of 1 to 10 so you have the index to the list. This is an unnecessary step because if the command were

random number from 1 to n

it avoids two math operations per repeat (one subtraction and one addition) and it's significantly faster OMM.


random number: Generate a random number
random number number -- the upper limit; if not specified, result is a real between 0 and 1
[from number] -- the lowest number to return
[to number] -- the greatest number to return (up to but not including)
[with seed number] -- a starting point for a repeatable sequence of random numbers
Result: number -- a real number between 0 and 1, or an integer between the from and to values provided


In script B you have no argument for the command, and now it returns a result that is a real between 0 and 1. You HAVE to coerce it to an integer in that script else you have no index number for the items in TheList. I didn't test either in X, but the two scripts take different approaches to the problem, and without the addition of the 0.05 factor to accommodate rounding, script B fails in OS 9 too.

- web


------------------------------ SCRIPT A
-------------------------------------------

set TheList to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

set N to the count of items in TheList

repeat 1000 times
random number N - 1
set ThisNumber to the result + 1
set item ThisNumber of TheList to (item ThisNumber of TheList) + 1
end repeat

TheList


--> {1524, 1000, 1017, 1015, 1014, 1026, 977, 930, 1006, 491}

--------------------------------- SCRIPT B
---------------------------------------

set TheList to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

repeat 100000 times
random number
set ThisNumber to {(the result + 0.05) * 10} as integer
set item ThisNumber of TheList to (item ThisNumber of TheList) + 1
end repeat

TheList

--> {10018, 10107, 10126, 9911, 10072, 10084, 10208, 9843, 9892, 9739}


  • Follow-Ups:
    • Re: Random number generator -- OS X different
      • From: Daniel Robinson <email@hidden>
References: 
 >Re: Random number generator -- OS X different (From: Daniel Robinson <email@hidden>)

  • Prev by Date: Command line args in MacOS X
  • Next by Date: Re: Random number generator -- OS X different
  • Previous by thread: Re: Random number generator -- OS X different
  • Next by thread: Re: Random number generator -- OS X different
  • Index(es):
    • Date
    • Thread