Page 1 of 1

Generating Random Numbers???

Posted: Sat Dec 28, 2013 1:33 pm
by Robindinho
Hi, I have 2 fields, a minimum and a maximum.

User's enter a number into each field and on the click of a button a random number between the min and max is generated into a field.

a). what is the code for a random variable between a max and min?

b). How can i make sure only a number is able to be entered into a field?

Re: How Do I Create A Random Variable?

Posted: Sat Dec 28, 2013 2:37 pm
by snm
Didn't you check it in Dictionary?
http://docs.runrev.com/Function/random
http://docs.runrev.com/Message/keyDown
I hope both links will answer your questions.
Marek

Re: Generating Random Numbers???

Posted: Sat Dec 28, 2013 2:59 pm
by Robindinho
thanks, bit of a newb on livecode only just found there was a dictionary, :)

Re: Generating Random Numbers???

Posted: Sat Dec 28, 2013 7:47 pm
by Dixie
hi...

put these two handlers into say a buttoon script... change the numbers in line 'put randomInRange( x, y) and see what is returned...:-) x being the lower number and y the higher...

Code: Select all

on mouseUp
   put randomInRange (3,10)
end mouseUp

function randomInRange lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 8:53 am
by carel
Dixie,

Could you or anyone else explain this code please:

return random(upperLimit - lowerLimit + 1) + lowerLimit - 1

I know what it does but I don't understand it. I use similar code to remove spaces in front of a string which I also do not understand. (the -1 of ...)

Thanks,

Carel

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 3:01 pm
by Ledigimate
Hi Carel

LiveCode's random function always returns a random number in the range of 1 to n, where n is the upper limit you pass to it.
But let's say for example that you want to return a number in the range of 5 to 10, then you can't simply return random(10), because it's lower limit would be 1 instead of 5.
You also can't simply return random(5) because it's maximum would be 5 instead of 10.
So to achieve a random number in the range of 5 to 10, you get a random number from the number of possibilities and then add the lower limit - 1.
For my example, the formula would thus be:
return random(10 - 5 + 1) + 5 - 1
Your original formula uses the variables lowerLimit and upperLimit instead of the constants 5 and 10.

I hope this helps.

Regards,

Gerrie

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 3:51 pm
by dunbarx
What Gerrie said.

To break it down just a little, though, think of this simpler case, where you want a random number between 5 and 10:

put random(5) + 5

What is just a little bit wrong with this? Please play with it, and write back with what you find. Then we (or you by then) can see what the more complex but very similar code actually does.

Craig Newman

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 5:17 pm
by carel
Gerrie and Craig,

That was not really my question, I did not know that it was "maths" happening.

The formula would return random(6) + 4, and Craig you say there is a problem with random(5) + 5 - it will never return 5 ?

The -1 confused me initially because of this:

function Trim pText
return word 1 to -1 of pText -- <-- I just don't get it - livecode is supposed to be human readable - what is word -1 ??
end Trim

Thanks,

Carel

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 6:35 pm
by Klaus
Hi Carel,

word -1
char -1
line -1
= the LAST word/char/line etc.
Livecode lets us count starting from the end of whatever :D
Craig you say there is a problem with random(5) + 5 - it will never return 5 ?
Please read up Craigs posting again, he did not say that :D


Best

Klaus

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 7:17 pm
by Ledigimate
Carel,

Klaus is right about the -1. "Word 1 to -1" basically means the first word to the last word.
Say, for instance, pText contains the string " The lazy dog couldn't care less about a jumping fox. ", and you wanted to return a range of words starting at the fifth word and ending with the second-to-last word, then the code for it would be
return word 5 to -2 of pText
which would return the string "care less about a jumping"

About what Craig said, you were actually right. Craig wanted us to see what's wrong with the seemingly simpler approach, which is that it would never return 5.
Let's play with it a bit more: Subtracting 1 from the second number would allow it to return 5, but then it would never return 10:
put random(5) + 4
To solve that, we add 1 to the first number:
put random(6) + 4
In this case 6 is the number of possible values in the range 5 to 10.

Regards,

Gerrie

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 8:12 pm
by dunbarx
Gerrie.
put random(6) + 4
This sort of "kluge" is adorable. The problem with it is that it lacks readability, in that it is not obvious what the bounds are. That is a matter of style, however.

Craig

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 9:21 pm
by Ledigimate
Yes,
random(10 - 5 + 1) + 5 - 1
is a bit more readable.
My apologies :oops:

Gerrie

Re: Generating Random Numbers???

Posted: Wed Feb 05, 2014 9:32 pm
by dunbarx
A blushing emoticon? Cummon, it wasn't that bad.

Craig

Re: Generating Random Numbers???

Posted: Thu Feb 06, 2014 8:43 am
by carel
Thanks everyone - now I get the word/char -1 :)