Solved

Random numbers

Posted on 1998-09-19
5
223 Views
Last Modified: 2010-04-01
I'm writting a simple game in which the user has to touch an item that ramdomly moves to a different place on the screen each time it is touched.  Right now the game is incredibly too easy because the object always comes back into the game in a spot very close to the place it was already at.  Here is the code I used:
//begin code
#include <time.h>
time_t seconds;

time(&seconds);
srand((unsigned int) seconds);

xposition = rand() % (HIGH - LOW + 1) + LOW;
yposition = rand() % (HIGH - LOW + 1) + LOW;
//end code
Does anyone know a better way to get a random number that isn't always close to the number it was before?
0
Comment
Question by:SileNcer
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 10 total points
Comment Utility
How about using only the high parameter...as follows

xposition = rand() % (HIGH);
yposition = rand() % (HIGH);

If you supply which is the Height and which is the Width it would be even easier to supply an answer..

xposition = rand() % (WIDTH);
yposition = rand() % (HEIGHT);

This is a better solution...I don't know what your game is so...this would be iot..

;/Viktor
0
 

Author Comment

by:SileNcer
Comment Utility
That made the code look better, but the object is still being ramdomized in a spot close to where it was before.  I was trying to make a Nibbles game (your a worm and you have to eat little blocks.)
I think the problem is that when the worm eats the block, the randomizing seed is too close to the previous in the split second the process occurs. Maybe you can help me now?
0
 

Author Comment

by:SileNcer
Comment Utility
Okay, I fixed it!!!  It seems that the x value wasn't really ramdomizing so I just did this:

xposition = rand() % (WIDTH);
yposition = rand() % (HEIGHT);
xposition = rand() % (WIDTH);

By putting in the ramdom x value twice, it worked :) don't know why.  Thanks a lot for your help!!!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Are you doing the srand every time you get a new position?
If so, that's probably the problem.
You should call srand just once at the start, then just repeatedly call rand() without reseeding it again.
0
 

Author Comment

by:SileNcer
Comment Utility
That worked!  Yeah, I guess I didn't need that in the function.  Thanks for helping me.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now