Solved

Random numbers

Posted on 1998-09-19
5
226 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
ID: 1173172
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
ID: 1173173
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
ID: 1173174
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
ID: 1173175
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
ID: 1173176
That worked!  Yeah, I guess I didn't need that in the function.  Thanks for helping me.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 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

13 Experts available now in Live!

Get 1:1 Help Now