## Generating Random Numbers???

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

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

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

### Generating Random Numbers???

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
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

### Re: How Do I Create A Random Variable?

Didn't you check it in Dictionary?
http://docs.runrev.com/Function/random
http://docs.runrev.com/Message/keyDown
Marek

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

### Re: Generating Random Numbers???

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

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

### Re: Generating Random Numbers???

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???

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
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

### Re: Generating Random Numbers???

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
Posts: 6518
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Generating Random Numbers???

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???

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: 11579
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Generating Random Numbers???

Hi Carel,

word -1
char -1
line -1
= the LAST word/char/line etc.
Livecode lets us count starting from the end of whatever
Craig you say there is a problem with random(5) + 5 - it will never return 5 ?

Best

Klaus

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

### Re: Generating Random Numbers???

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
Posts: 6518
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Generating Random Numbers???

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
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

### Re: Generating Random Numbers???

Yes,
random(10 - 5 + 1) + 5 - 1
My apologies

Gerrie
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

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

### Re: Generating Random Numbers???

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???

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