How to generate Math.Run from a value other than 0?

Asked

Viewed 1,167 times

1

Hey guys, I just got to know the Math.Andom method and I already know how to generate values from 0 to another number through multiplication, e.g.: Math.Random()*20.

But I’m wondering how I can do the same thing being that starting for example from 5 until 20, already swept the net and nobody explains :/

I appreciate anyone who can help me!

  • see my response with an auxiliary function that can serve you.

2 answers

2

The Math.() is by definition a number between 0 and 1. What you can do is add the value you want to have the result you want. That is to say:

var numeroAleatorio = 5 + (Math.random() * 15);

Thus ensures that the lower possible result is 5, and the higher possible is 20.

Namely the 5 in this case changes the minimum that this code can give, and the 15 in this case, added with the minimum, will be the maximum that the code can give.

  • Thank you Sergio. But how do you not pass 20?

  • 1

    @ropbla9 being that the maximum numet that the Math.round() gives is 1 then the maximum number of this line I’ve put in is 20. IE 5 + (1 * 15)

  • And if I wanted to draw only 5 to 15?

  • 1

    If you want to 5 to 15 can do var numeroAleatorio = 5 + (Math.random() * 10);

  • Put, no words. Thank you! The laziness of thinking here is too much, I’m sleeping but I only go to sleep when I finish my notes :/

2

You can use the following functions:

Returns a random number between min (inclusive) and max (exclusive):

function getNumeroRandom(min, max) {
    return Math.random() * (max - min) + min;
}

Returns a random number between min (inclusive) and max (inclusive):

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Obs: Using Math.round() will give you a non-uniform distribution, but with the getRandomInt() function you have a perfectly uniform distribution.

  • Thanks man for the method! It is one more in my range. However the method of Sergio above the same effect and is much more practical! Or you disagree?

  • @ropbla9 daria no same is done yes.. but that has a function with parameters, you can set them when calling it passing the values min and max(so "dynamic")... the other method the values are static and have to keep changing them when you want to make the draw between the two numbers.

  • Peter, but with that method I can also set 1 + and *19 as parameters so that they stay dynamic in the same way. Or not?

  • @ropbla9 can yes.. but you will be changing to only one specific case ... and with function you attend all cases, because it gets something more generalized.

  • 1

    @ropbla9 one thing is to create a variable.. and another and a function that is something general that can be used more than once regardless of the values avoiding duplication..

  • The point in having a function is to avoid duplication and decrease the chance of error!

  • @renatoargh juxtapose this aew.

  • But in the same way I can fit 1+ and 19* as parameters of a function, Ué

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.