Page 1 of 1

how to make a random variable value?

PostPosted: Wed Jan 16, 2008 5:11 pm
by Sondise
There's a variable, and i'd like to put its value as random between 0 and 10. How to do that?

Re: how to make a random variable value?

PostPosted: Wed Jan 16, 2008 7:12 pm
by asmodeus
Try this:
Code: Select all
my_variable = rand(10);

Re: how to make a random variable value?

PostPosted: Wed Jan 16, 2008 8:22 pm
by Sondise
didn't work the var is always the same value. Nice new avatar! :D

Re: how to make a random variable value?

PostPosted: Wed Jan 16, 2008 8:35 pm
by asmodeus
:?: But normally this script should work.

Re: how to make a random variable value?

PostPosted: Wed Jan 16, 2008 8:56 pm
by Sondise
I put this:

MouseButtonDown ->
my_variable = rand(10);
if (my_variable == 0 || 2 || 4 || 6 || 8 || 10)
{
xvelocity = -5;
}
else if (my_variable == 1 || 3 || 5 || 7 || 9)
{
yvelocity = -5;
}

Re: how to make a random variable value?

PostPosted: Thu Jan 17, 2008 12:10 am
by DarkParadox
replace the "My variable" and put the variable your using.
use this code

Code: Select all
my variable = rand(10);
if (my variable == 0 ||my variable == 2 ||my variable == 4 ||my variable == 6 ||my variable == 8 ||my variable == 10)
{
xvelocity = -5;
}
else if (my variable == 1 ||my variable == 3 ||my variable == 5 ||my variable == 7 ||my variable == 9)
{
yvelocity = -5;
}

Re: how to make a random variable value?

PostPosted: Thu Jan 17, 2008 12:12 am
by Game A Gogo
Code: Select all
if (my_variable == 0 || my_variable ==2 || my_variable == 4 || my_variable == 6 || my_variable == 8 || my_variable == 10)
{
xvelocity = -5;
}
else if (my_variable == 1 || my_variable ==3 || my_variable == 5 || my_variable == 7 || my_variable == 9)
{
yvelocity = -5;
}

would be the correct syntax, but if you make it with "random(2);" and do this code instead:
Code: Select all
if (my_variable == 0)
{
xvelocity = -5;
}
else if (my_variable == 1)
{
yvelocity = -5;
}
and would be just as efficient, also requires less CPU

Re: how to make a random variable value?

PostPosted: Thu Jan 17, 2008 12:14 am
by DarkParadox
uuummm... not really look sondise was trying to do this :
my_variable = rand(10);
if (my_variable == 0 || 2 || 4 || 6 || 8 || 10)
{
xvelocity = -5;
}
else if (my_variable == 1 || 3 || 5 || 7 || 9)
{
yvelocity = -5;
}

i was giving the code in complete form.

Re: how to make a random variable value?

PostPosted: Sat Jan 19, 2008 8:21 am
by Fuzzy
diormeh wrote:uuummm... not really look sondise was trying to do this :
my_variable = rand(10);
if (my_variable == 0 || 2 || 4 || 6 || 8 || 10)
{
xvelocity = -5;
}
else if (my_variable == 1 || 3 || 5 || 7 || 9)
{
yvelocity = -5;
}

i was giving the code in complete form.


That is nonsensical code. It might look right to your eyes, but It sure doesnt to me, and I bet GE is even fussier.

Try this:
Code: Select all
my_variable = rand(10);
if (!my_variable & 1) // check to see if number is even
{
xvelocity = -5;
}
else if (my_variable & 1) check to see if number is odd
{
yvelocity = -5;
}


What this does is compares the variable to 0 or 1 in binary form. it gives them both equal lengths and compares them to 0 or 1. if the binary number matches(the 1 is called a mask) then true is returned by the if and the body of the if is carried out.

0101 == 5
0001 == 1
====
0001

see how the ones match up? 5 & 1 == 0001 which is the same as true.

[Edit] edited to correct a mistake.