Generating random number between -1 to 1 with C++

henrythechief

Distinguished
Aug 15, 2010
1
0
18,510
0
I'm making a pong game for an assignment in class and what I'm trying to do is when my ball starts, i want it's direction of my ball to be change randomly every time i press space-bar. some reason its keeps generating the same value over and over.
here's my code:

if ((BallXspeed == 0) && (BallYspeed == 0))
{
//restarts ball
if(IsKeyDown(' '))
{
srand ( time(NULL) );
iRandX = (((rand() % 200) / 200 ) * 2) - 1;
iRandY = (((rand() % 200) / 200 ) * 2) - 1;
BallXspeed = 3 * iRandX;
BallYspeed = 3 * iRandY;
}
}

hope i can get some help soon.
thanks.
 

truegenius

Distinguished
Oct 22, 2011
113
0
18,660
13
your program is complicated to me.

But if it is in c++ then to generate random number in a program you need to initiate randomize() function first then use random.

Example
// program to generate random number from 0 and 99.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{int a=0;
randomize();
a=random(100);
cout<<a;
getch();}

let me know if it was helpful.
 
Thread starter Similar threads Forum Replies Date
Adriane Emmah Apps General Discussion 1
T Apps General Discussion 2
K Apps General Discussion 1
H Apps General Discussion 2
A Apps General Discussion 2
K Apps General Discussion 2
hoover1979 Apps General Discussion 2
wanamingo Apps General Discussion 3
P Apps General Discussion 4
M Apps General Discussion 4
B Apps General Discussion 4
wanamingo Apps General Discussion 1
T Apps General Discussion 3
V Apps General Discussion 6
S Apps General Discussion 19
F Apps General Discussion 4
F Apps General Discussion 5
J Apps General Discussion 2
I Apps General Discussion 1
B Apps General Discussion 6

ASK THE COMMUNITY