Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random numbers

Posted on 1998-09-19
5
Medium Priority
?
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 40 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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