radom range? [solved]

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

radom range? [solved]

Post by sms5138 » Fri Jul 31, 2015 7:01 pm

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
Last edited by sms5138 on Fri Jul 31, 2015 8:00 pm, edited 1 time in total.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: radom range?

Post by jmburnod » Fri Jul 31, 2015 7:10 pm

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
https://alternatic.ch

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: radom range?

Post by FourthWorld » Fri Jul 31, 2015 7:30 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: radom range?

Post by FourthWorld » Fri Jul 31, 2015 7:32 pm

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)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: radom range?

Post by Simon » Fri Jul 31, 2015 7:37 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: radom range?

Post by sms5138 » Fri Jul 31, 2015 7:59 pm

these were all great suggestions! thank you so much!

they all worked great!

Post Reply