Page 1 of 1

radom range? [solved]

Posted: Fri Jul 31, 2015 7:01 pm
by sms5138
I'm trying to click a button and get a random number between 4 and 8.

I have had some luck using

Code: Select all

on mouseUp
answer random(8)
end mouseUp
but i'm curious if there is a way to exclude numbers 1 through 3?

thank you in advance!

-Sean

Re: radom range?

Posted: Fri Jul 31, 2015 7:10 pm
by jmburnod
Hi Sean,
but i'm curious if there is a way to exclude numbers 1 through 3?
Yes, here is a way

Code: Select all

on mouseUp
   repeat with i = 4 to 15
      put i & cr after tAvailableNum
   end repeat
   answer getRandomWithoutSomeNumber(tAvailableNum)
end mouseUp

function getRandomWithoutSomeNumber pAvailableNum
   put the num of lines of pAvailableNum into tNbl
   put random(tNbl) into tRandom
   return line tRandom of pAvailableNum
end getRandomWithoutSomeNumber
Best regards
Jean-Marc

Re: radom range?

Posted: Fri Jul 31, 2015 7:30 pm
by FourthWorld
Another option would be to get a random value for the scope of the range, and then add the minimum to it:

Code: Select all

get random(5)
return 3+it

Re: radom range?

Posted: Fri Jul 31, 2015 7:32 pm
by FourthWorld
Another option would be to get a random value for the scope of the range, and then add the minimum to it:

Code: Select all

get 3+random(5)

Re: radom range?

Posted: Fri Jul 31, 2015 7:37 pm
by Simon
The dictionary has this;
function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
Might as well learn how to chop the top off as well.

Simon

Re: radom range?

Posted: Fri Jul 31, 2015 7:59 pm
by sms5138
these were all great suggestions! thank you so much!

they all worked great!