Page 1 of 1

EOS help - random numbers

Posted: Wed Mar 10, 2021 10:10 am
by Xardas
So, I've been trying to wrap my head around the EOS editor, and... well, so far not going well. For me coding is like an extraterrestrial language translated into Chinese that I'm trying to read in English via Google Translate... I tried to look up how things work in google, but... the explanations tend to confuse me even more. Like, I figured out that if I want random numbers, I use mathrandom command. So, from another thread on this forum I got this code, with the addendum that I should just change the min / max numbers to suit my needs:

Code: Select all

var myNumber = Math.floor((Math.random() * 20) + 1);
console.log('The number is %s', myNumber)
Since this code is supposed to get a random number from 1 to 20, I figured if I wanted a number between 50-200, I just change it to this:

Code: Select all

var myNumber = Math.floor((Math.random() * 200) + 50);
console.log('The number is %s', myNumber)
Well, first test result gave me 244 as result, so I'm clearly misunderstanding how this works. Tried to google to see how this code actually works, but I got explanations like:
The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Which... I don't understand anything of. I've never had a mind for math, so this whole thing just makes my head hurt. I just want to know how to do a basic random.org roll without having to link players to random-org...

I also wanted to figure out how to do a complex score system, like the ones in my lottery teases, but I gave up on that real quick. If I can't even get a random number, I'm clearly too dumb for this...

Re: EOS help - random numbers

Posted: Wed Mar 10, 2021 10:52 am
by Roblsforbobls
Hi Xardes,

I've been using the same code you used, but I never had a reason to change the minimum number :lol: good to know it only works if the minimum number is 1.
Check out this thread, one of the comments gives a bit of code where the minimum number can be changed:
https://stackoverflow.com/questions/495 ... avascript
I am on mobile so I haven't tested this yet, but hopefully it works for you:

Code: Select all

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}
Also I want to encourage you not to give up on Eos, because everyone starts somewhere! It takes time to get used to, and you'll pick up useful pieces of code one at a time. I recommend that as you find them you save each one to a page in a tease on your editor with a description of how they work. I have a Useful Code/Tricks tease on my editor that I reference regularly.
If you have questions on how to do something in the editor, feel free to reach out to me. My coding skills are non-existent but I might still be able to help. Otherwise I'm sure other users on the site would be willing to give the same offer :yes:

Re: EOS help - random numbers

Posted: Wed Mar 10, 2021 12:50 pm
by FeetTickler
Hi !

Just to explain a little bit how it actually works.

Math.random() generates a real with a lot of decimals between 0 and 1. Then with that you need to do some math to extrapolate it to what you want. So if you want a number between 0 and 50 you do

Code: Select all

Math.floor(Math.random() * 51)
Since math.floor will round this number down you need to add one.

Then if you want a number between two fixed boundaries you do something like

Code: Select all

Math.floor(Math.random() * (max - min + 1)) + min
(+1 still for the same reason as above)

So you get a random number between 0 and max - min (for example if you want a number between 20 and 50 you take a number between 0 and 30 and then you add 20 to that so it gives you a number between 20 and 50 (0 + 20 and 30 + 20 respectively)

Hope that helps you understand :)

Re: EOS help - random numbers

Posted: Thu Mar 11, 2021 6:10 am
by Xardas
Roblsforbobls wrote: Wed Mar 10, 2021 10:52 am
Also I want to encourage you not to give up on Eos, because everyone starts somewhere! It takes time to get used to, and you'll pick up useful pieces of code one at a time. I recommend that as you find them you save each one to a page in a tease on your editor with a description of how they work. I have a Useful Code/Tricks tease on my editor that I reference regularly.
If you have questions on how to do something in the editor, feel free to reach out to me. My coding skills are non-existent but I might still be able to help. Otherwise I'm sure other users on the site would be willing to give the same offer :yes:
Thank you! Also, I'm not giving up on EOS, I'm giving up on trying to automate a complex scoring system, since I have no idea how to even begin doing that for now. Though, I suppose I could try describing what I want and maybe someone can give me advice on it...
FeetTickler wrote: Wed Mar 10, 2021 12:50 pm So you get a random number between 0 and max - min (for example if you want a number between 20 and 50 you take a number between 0 and 30 and then you add 20 to that so it gives you a number between 20 and 50 (0 + 20 and 30 + 20 respectively)

Hope that helps you understand :)
Okay, so if I'm getting this right, my problem was that the 1 in the code I copied wasn't the minimum number, like I assumed, it was a necessary addition to counter rounding down, which in turn is necessary, to avoid having a motherload of decimals.

So... using the code you provided me with:

Code: Select all

Math.floor(Math.random() * (151)) + 50
This will result in a random number between 50 and 200, right?

Or I have to write the whole thing out, something like:

Code: Select all

Math.floor(Math.random() * (200 - 50 + 1)) + 50

Re: EOS help - random numbers

Posted: Sat Mar 13, 2021 2:38 pm
by fapnip
Xardas wrote: Thu Mar 11, 2021 6:10 am Though, I suppose I could try describing what I want and maybe someone can give me advice on it...
Go for it. I'm sure someone here will chime in to help.
Xardas wrote: Thu Mar 11, 2021 6:10 am it was a necessary addition to counter rounding down, which in turn is necessary, to avoid having a motherload of decimals.
Yes. Math.random() returns a random decimal between 0 and 1, but never 1.
Xardas wrote: Thu Mar 11, 2021 6:10 am Okay, so if I'm getting this right, my problem was that the 1 in the code I copied wasn't the minimum number, like I assumed, it was a necessary addition to counter rounding down, which in turn is necessary, to avoid having a motherload of decimals.

So... using the code you provided me with:

Code: Select all

Math.floor(Math.random() * (151)) + 50
This will result in a random number between 50 and 200, right?

Or I have to write the whole thing out, something like:

Code: Select all

Math.floor(Math.random() * (200 - 50 + 1)) + 50
Both should work the same. Get a random integer between 0 and 150, then add 50. However, it's good practice to wrap things like that in a function and put that in your Init Script so you can re-use it later without having to think about it.

So, in this case, you'd put this in your init script:

Code: Select all

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}
Then later, you use it via:

Code: Select all

var myNumber = randomIntFromInterval(50, 200);
console.log('The number is %s', myNumber)

Re: EOS help - random numbers

Posted: Thu Apr 08, 2021 7:13 am
by devilkitten
fapnip wrote: Sat Mar 13, 2021 2:38 pm So, in this case, you'd put this in your init script:

Code: Select all

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}
Then later, you use it via:

Code: Select all

var myNumber = randomIntFromInterval(50, 200);
console.log('The number is %s', myNumber)
I'm trying to replicate this for the purposes of a dice roll on a single page, with whole numbers 1-6. Copy-pasted both codes (top into init script) and changed the bottom one's interval numbers. Currently, I'm tying the bottom code to a "roll dice" button, and then creating multiple if:then prompts for each number. For example:

Code: Select all

if: 
var mynumber === 1
then:
Say: You rolled a 1.
After running the preview of the page in question, it's stopping dead at the roll button and not doing anything. What is it that I'm doing wrong here? I have zero prior experience in coding of any kind, and this feels like it should make sense but just isn't working as I intend it to.

Re: EOS help - random numbers

Posted: Thu Apr 08, 2021 8:14 am
by MisterFlames
devilkitten wrote: Thu Apr 08, 2021 7:13 am After running the preview of the page in question, it's stopping dead at the roll button and not doing anything. What is it that I'm doing wrong here? I have zero prior experience in coding of any kind, and this feels like it should make sense but just isn't working as I intend it to.
Did you try it like this?:

Image
Image
Image

My result:
Image

Re: EOS help - random numbers

Posted: Thu Apr 08, 2021 2:58 pm
by fapnip
devilkitten wrote: Thu Apr 08, 2021 7:13 am

Code: Select all

if: 
var mynumber === 1
If you actually have var mynumber === 1 as the IF action's expression, that's the issue. Should only be mynumber === 1 (Drop the "var". You're not defining a variable here, only testing the value of an existing variable.)