How do i do random number between 20-30? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How do i do random number between 20-30?

How?

9th Dec 2018, 1:12 AM
Jingga Sona
Jingga Sona - avatar
23 Answers
+ 7
function randomInt(max,min) { return math.floor(math.random() * (max -min + 1)) + min } check this
9th Dec 2018, 2:33 AM
Reza Aghajani
Reza Aghajani - avatar
+ 4
just like return means when you call some functions return calculated value on function! :)
9th Dec 2018, 2:36 AM
Reza Aghajani
Reza Aghajani - avatar
+ 3
Reza Aghajani still not work, it say "undefined"
9th Dec 2018, 2:31 AM
Jingga Sona
Jingga Sona - avatar
+ 3
Thanks Reza Aghajani! I learn something new today
9th Dec 2018, 2:40 AM
Jingga Sona
Jingga Sona - avatar
+ 3
Your welcome!
9th Dec 2018, 2:42 AM
Reza Aghajani
Reza Aghajani - avatar
+ 3
Shivek Sahay i am front end developer so i dont do php Ramin as you can see in the tag, javascript
9th Dec 2018, 8:22 AM
Jingga Sona
Jingga Sona - avatar
+ 3
Coder yes i know in p5.js you can do random(x, y) but i can't do it in every case for example you cant do alert(random(5,10))
9th Dec 2018, 11:56 AM
Jingga Sona
Jingga Sona - avatar
+ 3
To write something in words that's implied by all the algorithms here: You want random numbers that range across a maximum of 10 values (it does not matter if that's 10-20 or 100-110, it's still 10 values), shifted into the correct spot (because now it it matters WHICH 10). My point is, generate in the range first, then shift. Any random function that generates an answer between 0 and 1 (like Math.random) is very easily interpreted as generating a PERCENTAGE, which is why you multiply a "random percentage between 0 and 1", or 'none to all of it") by your range. The exact same strategy (0...1 representing a percentage weight) is used in probabilities and machine learning.
9th Dec 2018, 5:54 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
#Language: Python import random x = random.randint(20,30) #storing in a variable print(x)
9th Dec 2018, 8:40 PM
Akindele Olawole
Akindele Olawole - avatar
+ 3
in java you can use this--> ----------------------------- import java.util.Random; --------------------------- Random(10)+20;
10th Dec 2018, 6:35 AM
Javlon
Javlon - avatar
+ 2
Reza Aghajani It doesnt work
9th Dec 2018, 2:12 AM
Jingga Sona
Jingga Sona - avatar
+ 2
Reza Aghajani perfect! Can you tell me what "return" do?
9th Dec 2018, 2:35 AM
Jingga Sona
Jingga Sona - avatar
+ 2
int rand(); The rand function takes no arguments and returns an integer that is a pseudo-random number between 0 and RAND_MAX. On transformer, RAND_MAX is 2147483647.
9th Dec 2018, 8:22 AM
Sam diaz
Sam diaz - avatar
+ 2
Ramin here is your answer Use this:- rand() % upperlimit + lowerlimit; Here upperlimit =10 and lowerlimit = 1. So , adjust according to your need.
9th Dec 2018, 8:33 AM
Sam diaz
Sam diaz - avatar
+ 2
int number=(int)((Math.pow()*(n-m))+m); Here, 'n' is the upper limit and 'm' is the lower limit... So, to create random numbers between 20 and 30 will be: int number=(int)((Math.pow()*(30-20))+20);
9th Dec 2018, 10:50 AM
mn121
mn121 - avatar
+ 2
Just like Reza, but remember math library with Upercase: function randomInt(max,min) { return Math.floor(Math.random() * (max -min + 1)) + min }
11th Dec 2018, 4:46 PM
DFrancos
DFrancos - avatar
+ 1
by using pseudo random keyword ,rand()
9th Dec 2018, 4:30 AM
samridhi dwivedi
samridhi dwivedi - avatar
+ 1
Can any one give me the full code in C++?
9th Dec 2018, 6:32 PM
Mr Robot
Mr Robot - avatar
+ 1
math. random()
9th Dec 2018, 6:35 PM
Jinde Sandeep
Jinde Sandeep - avatar
+ 1
سلام
9th Dec 2018, 7:07 PM
سالار سرمدی
سالار سرمدی - avatar