how to make a random variable value?

Talk about making games.

how to make a random variable value?

Postby Sondise » Wed Jan 16, 2008 5:11 pm

There's a variable, and i'd like to put its value as random between 0 and 10. How to do that?
User avatar
Sondise
 
Posts: 94
Joined: Thu Nov 01, 2007 11:04 pm
Location: South america
Score: 0 Give a positive score

Re: how to make a random variable value?

Postby asmodeus » Wed Jan 16, 2008 7:12 pm

Try this:
Code: Select all
my_variable = rand(10);
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: how to make a random variable value?

Postby Sondise » Wed Jan 16, 2008 8:22 pm

didn't work the var is always the same value. Nice new avatar! :D
User avatar
Sondise
 
Posts: 94
Joined: Thu Nov 01, 2007 11:04 pm
Location: South america
Score: 0 Give a positive score

Re: how to make a random variable value?

Postby asmodeus » Wed Jan 16, 2008 8:35 pm

:?: But normally this script should work.
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: how to make a random variable value?

Postby Sondise » Wed Jan 16, 2008 8:56 pm

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;
}
User avatar
Sondise
 
Posts: 94
Joined: Thu Nov 01, 2007 11:04 pm
Location: South america
Score: 0 Give a positive score

Re: how to make a random variable value?

Postby DarkParadox » Thu Jan 17, 2008 12:10 am

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;
}
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: how to make a random variable value?

Postby Game A Gogo » Thu Jan 17, 2008 12:12 am

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: how to make a random variable value?

Postby DarkParadox » Thu Jan 17, 2008 12:14 am

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.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: how to make a random variable value?

Postby Fuzzy » Sat Jan 19, 2008 8:21 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron