GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2004, 23:02
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough

rand() giving the same value


Hey, I have a program that I'll be posting in the Source Code/Tutorials section, and I'm having a slight problem with it. It uses the rand() function from cstdlib to get a random number between 1 and 4 to determine which direction to move a distance of one unit. Here's the problem code:

CPP / C++ / C Code:
point NewPosition(point currentpos)
{
  int x = (rand() % 4) + 1; // Generates nothing but 3

  if (x == 1)
    {
      currentpos.y -= 1;   // Move to the left
      return currentpos;
    }
  else
    if (x == 2)
      {
        currentpos.y += 1; // Move to the right
        return currentpos;
      }
    else
      if (x == 3)
        {
          currentpos.x -= 1; // Move up
          return currentpos;
        }
      else
        {
          currentpos.x += 1; // Move down
          return currentpos;
        }
}

The point datatype consists of just two integers named x and y. Now... I'm really wondering why I'm only getting the number three when I should be getting random numbers between 1 and 4.
  #2  
Old 02-Mar-2004, 23:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Aaron, you need to seed the random generator with a call to srand(int). Take a look at this thread and its links for a complete discussion on the rand() function.

rand() is decidedely unrandom. For that reason, a seed of the current time is used often times to give the appearance of randomness. For what you are trying to do though you could probably just throw it a static int. The pattern will always be the same though.
  #3  
Old 02-Mar-2004, 23:19
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
The seed! Of course! I forgot about that. Could you possibly show me how to get the current time so that I could seed the rand() function with it?

EDIT: Nevermind, I found it in the thread you referred me to. Thanks : )
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.