How can I generate a random number between two values, such as 0 and 4 in JavaScript and set that random number as a variable??
–Thanks!!
How can I generate a random number between two values, such as 0 and 4 in JavaScript and set that random number as a variable??
–Thanks!!
That’s in the docs.
–Your Welcome!!
var x = Random.Range(0, 4);
Thanks, but I seem to have this compiling error with my code…
ArgumentException: RandomRangeInt can only be called from the main thread.
var random = Random.Range(0, 4);
function Start ()
{
print (random);
}
Random is an instruction
while random is a value, a float to be specific.
Therefore, what is missing in your code is…
that you need to declare var random as a float.
var random : float = Random.Range(0, 4);
should do.
You can’t run code like that when declaring variables outside of functions. Stick to declaring variables only outside of functions, and running code inside of functions.
This is incorrect, please read the docs. Speaking of which, Random.Range(0, 4) when using integers will only result in possible values between 0 and 3; again this is covered in the docs.
–Eric
Oh, sorry.
I thought the missing element was the fact he had not declard the vars type. I have never used random. My apologies.
You never need to declare the type of a variable, since it will be inferred from the value supplied. Although it’s a good idea whenever it’s not immediately obvious what the type is.
–Eric
The correct thing to do is
var randomNumber : int;
function Start ()
{
var random: int = Random.Range(0, 4);
randomNumber = random;
print (randomNumber );
}
Thanks pezz!!
No problem
actually there’s no need for the creation of additional variable or double assignment …
var randomNumber : int;
function Start ()
{
randomNumber = Random.Range(0, 4);
print (randomNumber );
}
I did that so he would have some output in the editor. As he initially wanted.
im talking about this
var random: int = Random.Range(0, 4);
randomNumber = random;
ooohhh I just noticed hat you are talking about
all good
im all about minimal, clean code