Random number help

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Random number help

Post by codinitup » June 27th, 2012, 8:22 pm

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.
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

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: Random number help

Post by npissoawsome » June 27th, 2012, 9:06 pm

codinitup 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.
do a random check to invert them... so like

Code: Select all

int c

c = rand() % 2;
if(rand == 1)
   x = -x;

c = rand() % 2;
if(rand == 1)
   y = -y;

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: Random number help

Post by codinitup » June 27th, 2012, 10:13 pm

Well I did something similar

Code: Select all

int c = rand() % (8);
	x = x + c;
	y = y + c;
	if (c == 1)
	{
		x = - x;
	}
however it did not work
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Random number help

Post by LuX » June 27th, 2012, 10:33 pm

Code: Select all

int c = rand() % (8);
   x = x + c;
   y = y + c;
   if (c == 1)
   {
      x = - x;
   }
Well, what did you expect that code to do?

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...
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: Random number help

Post by codinitup » June 27th, 2012, 10:44 pm

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,

Code: Select all

x = rand() % 8;
y = rand() % 8;
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

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

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Random number help

Post by LuX » June 27th, 2012, 11:53 pm

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
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: Random number help

Post by codinitup » June 28th, 2012, 12:17 am

I posted the downloadable file to the first post on this thread, I can't seem to find the problem! Please help
MOOOOOO

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: Random number help

Post by npissoawsome » June 28th, 2012, 4:17 am

This will give you a random number between -6 and positive 6

Code: Select all

int x = (rand() % 13)-6;
int y = (rand() % 13)-6;
doing

Code: Select all

int v = rand() % 7;
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

Code: Select all

rand() % 50;
it would only give me a number between 0-49, I explained why above

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Random number help

Post by LuX » June 28th, 2012, 9:28 am

Right, thanks for letting me know.
On VB the numbers where always from 0 to the number requested.
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Random number help

Post by Asimov » June 28th, 2012, 8:50 pm

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.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Post Reply