Can I change the seed for Random Number

Hey all,


I like to 'play' with applescript with the idea being that I understand the foundation with something easy that I can use later.


Here is a very simple script I wrote for choosing who should go to lunch first within our IT team:


Header 1

display dialog "Ready to decide who gets lunch?" buttons {"No", "Yes"} default button 2

if button returned of result is "Yes" then

set RN to (random number from 1 to 100)

if RN is less than or equal to 50 then

display dialog "Andy gets first lunch" buttons {"Awesome"} default button 1

end if

if RN is greater than or equal to 51 then

display dialog "Bill gets first lunch" buttons {"Awesome"} default button 1

end if

else


display dialog "You'll Never Know" buttons {":/"} default button 1

end if


delay 2

display dialogRNbuttons {"Thanks"} default button 1


What I have found though is that the numbers generated can be the same at least two times in a row. At a minimum it seems predictable. Now, I understand that over time this would probably even out but since I am running this script only once a day it seems pretty lopsided to my colleague (and not that he cares but 'its not fair'). So I was wondering if there is a way for me to randomize the seed for Random Number? Or is there a .plist that I can reset if its just a software bug?


Thanks,


-Bill

Mac mini, OS X Server, 10.9

Posted on Jan 2, 2015 10:59 AM

Reply
6 replies

Jan 4, 2015 9:06 PM in response to shirtlessbill

Consider your script.


You ask AppleScript for:


set RN to (random number from 1 to 100)


and then check to see if it's greater or less than 50 - it's not going to be hard for "greater than 50" to come up twice (or more) in a row. I'd be surprised if you got the same number twice, but not if you got "any number between 1 and 50" multiple times.


In either case, you can improve your 'randomness' significantly by not limiting your range:


set RN to random number


This will return a real (vs. integer) number between 0.000000000001 and 0.999999999999 - a far wider range, with a much smaller chance of collisions (and you could compare to (> 0.5) to retain your same two options.

Jan 5, 2015 7:31 AM in response to shirtlessbill

What I have found though is that the numbers generated can be the same at least two times in a row.

There is nothing wrong with that. You might need to take a quick refresher in what random numbers mean,


Lets take a simple example, a cost toss (which is basically what you are doing here). In any run of coin tosses there will be many runs of heads or tails, in fact if these runs did not show up it would be an indication that the random number generator is not random or someone is trying to game the system 😀


Now most computer random number generators at this level, something supplied by supplied by the OS for simple programs like this are not truly random and if run multiple times will produce the same results but within the individual runs the numbers will be random enough for most uses.


In your example you could really just take a random number between 0 and 1 (either 0 or 1) it would make no difference. Try this


set {heads, tails} to {0, 0}


repeat 1000 times

set n to random numberfrom 0 to 100

if n ≥ 50 then

set heads to heads + 1

else

set tails to tails + 1

end if

end repeat

log "random number 0 to 100"

logheads

logtails


set {heads, tails} to {0, 0}


repeat 1000 times

set n to random numberfrom 0 to 1

if n is 0 then

set heads to heads + 1

else

set tails to tails + 1

end if

end repeat

log "random number 0 to 1"

logheads

logtails


set {heads, tails} to {0, 0}


repeat 1000 times

set n to random number

if n ≥ 0.5 then

set heads to heads + 1

else

set tails to tails + 1

end if

end repeat

log "random number"

logheads

logtails


you;ll see the output for all three is the same (taking into account the randomness)


regards


<edit>

as I wrote below there is a small statistical advantage to the group than includes 50. That is if you test to see if the number is great than or equal to 50 then that group will come up a bit more often. If you tested the other way less then or equal to 50 then that group comes up a bit more.


When making binary decisions t is best to simply choose either 0 or 1 and then there will be no bias.

Jan 5, 2015 7:28 AM in response to Camelot

In either case, you can improve your 'randomness' significantly by not limiting your range:


set RN to random number


This will return a real (vs. integer) number between 0.000000000001 and 0.999999999999 - a far wider range, with a much smaller chance of collisions (and you could compare to (> 0.5) to retain your same two options.

In the simple case where you are simply looking to make a binary decision on the outcome it will make no difference if you go from 0 to 1, 0 to 100 or as you did in this case. Actually that is not 100% accurate as one or the other ranges will have one extra number depending on how you test (≥ 50, or ≤ 50) one group will have one more digit so that group will be slightly more likely to come up.


regards

Jan 5, 2015 11:09 AM in response to shirtlessbill

Hello


Randomness in selection and diversity in selection are two things. For instance, when you're planning lunch menu and don't want to have the same menu in a row or in a short period of time, you need to control the menu selection probability based upon the selection history.


I have played about with this and come to something like the following script.


Have fun.

H



--APPLESCRIPT property qq : {} -- selection history, stored in right-to-left order (left end is the latest) set aa to "abcde"'s characters diverse_selection(aa, {memory:0.8, penalty:0.7}) on diverse_selection(aa, opts) (* list aa : source list of entities record opts : {memory: a, penalty: b} (real) a : memory coefficient in [0, 1); default a = 0.7 (real) b : penalty coefficient in [0, 1); default b = 0.6 (property qq : selection history, stored in right-to-left order (left end is the latest)) *) (* Given entities P = {i : i = 1..m}, last-k selection history Q = {q_j : j = 1..k, k <= n, q_j in P}, the greater j denotes the older; entity i's next selection weight w_i is calculated by w_i = 1 - c * (∑ (a ^ j) : for all j such that q_j = i) / ak where ak = ∑ (a ^ j) : j = 1..k = a * (1 - a ^ k) / (1 - a) a is memory coefficient in [0, 1) b is penalty coefficient in [0, 1) c = b * bx bx = (∑ (a ^ j) : j = 1..m) / a ^ m = a * (1 - a ^ m) / (1 - a) / a ^ m and its selection probability s_i is calculated by s_i = w_i / (∑ w_j : for all j in P and w_j > 0), if w_i > 0; = 0, otherwise *) script o property pp : aa -- entities property m : count my pp -- number of entities property n : m * 3 -- max length of history property op : opts & {memory:0.7, penalty:0.6} property a : op's memory -- memory coefficient : [0, 1) property b : op's penalty -- penalty coefficient : [0, 1) property bx : a * (1 - a ^ m) / (1 - a) / (a ^ m) -- penalty supremum (not-inclusive) to guarantee at least one selection property c : b * bx -- penalty : [0, bx) property cc : {} -- penalty weight for entities considering history property ww : {} -- selection weight for entities considering history if a < 0 or a ≥ 1 then error "memory coefficient must be in [0, 1) but given: " & a number 8000 if b < 0 or b ≥ 1 then error "penalty coefficient must be in [0, 1) but given: " & b number 8000 if (count my qq) > n then set qq to my qq's items 1 thru n set k to count my qq repeat m times set my cc's end to 0 set my ww's end to 0 end repeat set ak to a * (1 - a ^ k) / (1 - a) -- an (penalty weight normaliser) = ∑ (a ^ j) : j = 1..k repeat with j from 1 to k set i to my qq's item j set my cc's item i to (my cc's item i) + (a ^ j) / ak -- cc[i] = ∑ (a ^ j) / ak : i = qq[j] end repeat repeat with i from 1 to count my ww set my ww's item i to 1 - c * (my cc's item i) end repeat set wm to 0 repeat with w in my ww set w to w's contents if w > 0 then set wm to wm + w -- wm (selection weight normaliser) = ∑ w : w > 0 end repeat if wm = 0 then error "internal error: penalty is likely too large." number 8001 set r to random number -- [0, 1] set s to 0 -- selected index set t to 0 -- current threashold [0, 1] repeat with i from 1 to count my ww set w to my ww's item i if w > 0 then set t to t + w / wm if r ≤ t then set s to i exit repeat end if end repeat if s = 0 then set s to -1 set p to my pp's item s set my qq's beginning to s return {p, s, qq, {a:a, b:b, c:c, bx:bx, ww:ww, cc:cc, wm:wm, r:r}} -- for test return p end script tell o to run end diverse_selection --END OF APPLESCRIPT

Jan 5, 2015 1:33 PM in response to Frank Caggiano

Frank, that's what I was trying to say - when you only have two options (less than 50/100 or greater than 50/100) you are likely to get sequences of the same result.


My comment on removing the range and getting a real result was more a reflection on the seeding comment - don't limit yourself to 100 possible values when you can have 1,000,000,000,000 choices. Neither of which would have any material impact on this particular use-case.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can I change the seed for Random Number

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.