+ 8
How do i do random number between 20-30?
How?
24 ответов
+ 7
function randomInt(max,min) {
return math.floor(math.random() * (max -min + 1)) + min
}
check this
+ 4
just like return means when you call some functions return calculated value on function! :)
+ 3
Reza Aghajani still not work, it say "undefined"
+ 3
Thanks Reza Aghajani! I learn something new today
+ 3
Your welcome!
+ 3
Shivek Sahay i am front end developer so i dont do php
Ramin as you can see in the tag, javascript
+ 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))
+ 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.
+ 3
#Language: Python
import random
x = random.randint(20,30) #storing in a variable
print(x)
+ 3
in java you can use this-->
-----------------------------
import java.util.Random;
---------------------------
Random(10)+20;
+ 2
Reza Aghajani It doesnt work
+ 2
Reza Aghajani perfect!
Can you tell me what "return" do?
+ 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.
+ 2
Ramin here is your answer
Use this:-
rand() % upperlimit + lowerlimit;
Here upperlimit =10 and lowerlimit = 1.
So , adjust according to your need.
+ 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);
+ 2
Just like Reza, but remember math library with Upercase:
function randomInt(max,min) {
return Math.floor(Math.random() * (max -min + 1)) + min
}
+ 1
by using pseudo random keyword ,rand()
+ 1
Can any one give me the full code in C++?
+ 1
math. random()
+ 1
سلام