Solved

Random numbers

Posted on 1998-09-19
5
224 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 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

22 Experts available now in Live!

Get 1:1 Help Now