[SOLVED] Get a random int between a large range

Hi!

I have an entity with about 70 children and I’m doing a “do…while” to select a random child and execute a function on its attached script, but I noticed that it generates a floating number…

How do I get a random number between 0-70, like 13 for example instead of 13.134234…?

:confused:

Maybe you can do that like this ?

Math.floor(Math.random() * 70)
1 Like

Ohh yes! I’ve thought it woudn’t work, but it did, tks! :slight_smile:

Note that the code from @scarlex returns a random integer between 0 and 69 inclusive. It can’t return 70 because Math.random never returns 1.

2 Likes

So you can multiply 71 ? :smile:

while (Math.floor(Math.random() * 71) !== 70)
  console.log('continue');
2 Likes

@will @scarlex

Hmmm good to know! Well, since I don’t want my last child to be considered cause it is a different type (text) of the remaining children (buttons) it will be perfect!
I just need to delete the -1 in my code: choose2 = Math.floor(Math.random() * (this.entity.children.length-1));

Ty both of you :slight_smile:

1 Like