Generating Random Numbers???

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Robindinho
Posts: 2
Joined: Sat Dec 28, 2013 1:31 pm

Generating Random Numbers???

Post by Robindinho » Sat Dec 28, 2013 1:33 pm

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?
Last edited by Robindinho on Sat Dec 28, 2013 2:37 pm, edited 1 time in total.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: How Do I Create A Random Variable?

Post by snm » Sat Dec 28, 2013 2:37 pm

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

Robindinho
Posts: 2
Joined: Sat Dec 28, 2013 1:31 pm

Re: Generating Random Numbers???

Post by Robindinho » Sat Dec 28, 2013 2:59 pm

thanks, bit of a newb on livecode only just found there was a dictionary, :)

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Generating Random Numbers???

Post by Dixie » Sat Dec 28, 2013 7:47 pm

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

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Generating Random Numbers???

Post by carel » Wed Feb 05, 2014 8:53 am

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

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Generating Random Numbers???

Post by Ledigimate » Wed Feb 05, 2014 3:01 pm

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
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Random Numbers???

Post by dunbarx » Wed Feb 05, 2014 3:51 pm

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

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Generating Random Numbers???

Post by carel » Wed Feb 05, 2014 5:17 pm

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

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Generating Random Numbers???

Post by Klaus » Wed Feb 05, 2014 6:35 pm

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

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Generating Random Numbers???

Post by Ledigimate » Wed Feb 05, 2014 7:17 pm

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
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Random Numbers???

Post by dunbarx » Wed Feb 05, 2014 8:12 pm

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

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Generating Random Numbers???

Post by Ledigimate » Wed Feb 05, 2014 9:21 pm

Yes,
random(10 - 5 + 1) + 5 - 1
is a bit more readable.
My apologies :oops:

Gerrie
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Random Numbers???

Post by dunbarx » Wed Feb 05, 2014 9:32 pm

A blushing emoticon? Cummon, it wasn't that bad.

Craig

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Generating Random Numbers???

Post by carel » Thu Feb 06, 2014 8:43 am

Thanks everyone - now I get the word/char -1 :)

Post Reply

Return to “Talking LiveCode”