Random number help
Random number help
Hey chili I have been having some trouble with a piece of code, I want to have a spaceship that has an x and y value along with r,g,b for color. I want it to move randomly in any direction. It currently only moves one direction and I can't fix it.
x = rand() % (7);
y = rand() % (7);
please help me get it to move completely randomly.
x = rand() % (7);
y = rand() % (7);
please help me get it to move completely randomly.
- Attachments
-
- MyGame.zip
- (41.69 KiB) Downloaded 153 times
Last edited by codinitup on June 28th, 2012, 12:15 am, edited 1 time in total.
MOOOOOO
- npissoawsome
- Posts: 114
- Joined: June 8th, 2012, 3:01 pm
Re: Random number help
do a random check to invert them... so likecodinitup wrote:Hey chili I have been having some trouble with a piece of code, I want to have a spaceship that has an x and y value along with r,g,b for color. I want it to move randomly in any direction. It currently only moves one direction and I can't fix it.
x = rand() % (7);
y = rand() % (7);
please help me get it to move completely randomly.
Code: Select all
int c
c = rand() % 2;
if(rand == 1)
x = -x;
c = rand() % 2;
if(rand == 1)
y = -y;
Re: Random number help
Well I did something similar
however it did not work
Code: Select all
int c = rand() % (8);
x = x + c;
y = y + c;
if (c == 1)
{
x = - x;
}
MOOOOOO
Re: Random number help
Code: Select all
int c = rand() % (8);
x = x + c;
y = y + c;
if (c == 1)
{
x = - x;
}
Brake it down to what it could be:
"rand() % 8;" no brackets needed really.
c can result as 0, 1, 2, 3, 4, 5, 6, 7, 8.
Lets say x = 50 and y = 40
so x can be 50, -50, 52, 53, 54, 55, 56, 57, 58
and y can be 40, 41, 42, 43, 44, 45, 46, 47, 48
Must be a weird rocket you have mostly moving diagonally bottom right and sometimes teleporting out of screen, etc...
ʕ •ᴥ•ʔ
Re: Random number help
lol the rocket's screwed up, but I need it to move randomly in every direction, for example I need it to move right and randomly switch and go to the left , I also want it to go left and randomly switch and start to go right, the same for up and down,
the above gives me 1-8, I need it to also give me a random negative value (-1 throguh - 8)
However I can't do this
because it starts going one way and stays going that way.
Any solutions?
Code: Select all
x = rand() % 8;
y = rand() % 8;
However I can't do this
Code: Select all
x = rand() % 8;
y = rand() % 8;
x = rand() % -8;
y = rand() % -8;
because it starts going one way and stays going that way.
Any solutions?
MOOOOOO
Re: Random number help
The above gives from 0 to 8! not 1 to 8.
The solution is pretty simple:
x = (rand() % 8) - 8; // this will give a number from -8 to 8
y = (rand() % 8) - 8; // this will give a number from -8 to 8
if you want just a negative, how about just make the number negative eg. x = ((rand() % 8) + 1) * -1; // this will give a number from -1 to -8. You cant use negative ints in rand since if x = -8 then 0 < -8 is incorrect...
or if you want decimal rands:
double x = (rand() % (int)(PI * 10000)) / 10000; // this will give a number from 0 to PI (if PI was defined, that is) with 0.0001 decimal
or maybe you want from 50 to 100:
int x = (rand() % 50) + 50;
------
Hope these will help : -P
The solution is pretty simple:
x = (rand() % 8) - 8; // this will give a number from -8 to 8
y = (rand() % 8) - 8; // this will give a number from -8 to 8
if you want just a negative, how about just make the number negative eg. x = ((rand() % 8) + 1) * -1; // this will give a number from -1 to -8. You cant use negative ints in rand since if x = -8 then 0 < -8 is incorrect...
or if you want decimal rands:
double x = (rand() % (int)(PI * 10000)) / 10000; // this will give a number from 0 to PI (if PI was defined, that is) with 0.0001 decimal
or maybe you want from 50 to 100:
int x = (rand() % 50) + 50;
------
Hope these will help : -P
ʕ •ᴥ•ʔ
Re: Random number help
I posted the downloadable file to the first post on this thread, I can't seem to find the problem! Please help
MOOOOOO
- npissoawsome
- Posts: 114
- Joined: June 8th, 2012, 3:01 pm
Re: Random number help
This will give you a random number between -6 and positive 6
doing
will return a number between 0 and 6
because it's the modulus, or remainder of the division
if I divide something by 7, the remainder can't be 7, because 7 goes into 7
so it cycles in a linear relationship like this
actual num:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, etc...
number after mod:
0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, etc...
...
also Lux, if I were to do
it would only give me a number between 0-49, I explained why above
Code: Select all
int x = (rand() % 13)-6;
int y = (rand() % 13)-6;
Code: Select all
int v = rand() % 7;
because it's the modulus, or remainder of the division
if I divide something by 7, the remainder can't be 7, because 7 goes into 7
so it cycles in a linear relationship like this
actual num:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, etc...
number after mod:
0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, etc...
...
also Lux, if I were to do
Code: Select all
rand() % 50;
Re: Random number help
Right, thanks for letting me know.
On VB the numbers where always from 0 to the number requested.
On VB the numbers where always from 0 to the number requested.
ʕ •ᴥ•ʔ
Re: Random number help
Hi all,
I have been reading to get a better random number you can set the seed to the clock time of your computer with something like
unsigned seed = time(0);
srand(seed);
and you have to include #include <time.h> to use it.
I am not 100% certain how it works, but it might help.
Asimov
PS I am not even sure yet why unsigned is used over int LOL, but there you go.
I have been reading to get a better random number you can set the seed to the clock time of your computer with something like
unsigned seed = time(0);
srand(seed);
and you have to include #include <time.h> to use it.
I am not 100% certain how it works, but it might help.
Asimov
PS I am not even sure yet why unsigned is used over int LOL, but there you go.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk