Skip to main content
GameDev.net gamedev.net
🔒 Locked

rand and srand are killing me

Started by Meta Adam Feb 6, 2005 at 1:48 PM 6 replies 1.8k views
Original Post
Meta Adam
Meta Adam
I have a question, I have 3 different rand()%10's inside 2 for loops, and the output is random numbers but they are the 3 same in a row,

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
union Block {
  int color, relxloc, relyloc;
};
Block board[10][10];
int main() {
  srand((unsigned)time( NULL ));
  for(int y=0;y<10;y++) {
    for(int x=0;x<10;x++) {
      board[x][y].color = rand()%10;;
      board[x][y].relxloc = rand()%10;;
      board[x][y].relyloc = rand()%10;;
      cout<<board[x][y].color<<" "<<board[x][y].relxloc<<" "<<board[x][y].relyloc<<endl;
    }
  }
  return 0;
}

and my output is like 111 666 333 888 666 222 666 222 times 100, for every cell on the board. How can I get it to change every time so its like 356 864 373 266 like that? Thanks.
Meta AdamOne of a million noob C++ programmers.
jfclavette
jfclavette
Humm.. you are using a union when what you want is a struct. You should read on those.

Just replace your "union Block" with "struct Block". It is however vital that you understand what they are before you proceed.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Meta Adam
Meta Adam
Yah, I know what a struct is but I thought I could get by with using one, but I guess I can't. Is it because a union is tied to one block of memory so if 1 of them gets edited the others do too?
Thought I was gonna be able to use less memory =).
Meta AdamOne of a million noob C++ programmers.
jfclavette
jfclavette
Yeah, a union is a defined type which contains only one of its member object at given time. since relxloc relyloc and color mapped to the same memory location, hence the output.

To be perfectly honest, I have never ever used a union, and I can see very few cases where they would be both really useful and easy to understand

Ah, and don't worry about memory just yet [grin]
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Meta Adam
Meta Adam
Hee, alright, Thanks!
Meta AdamOne of a million noob C++ programmers.
Zahlman
Zahlman
Quote:
Original post by xMcBaiNx
Yeah, a union is a defined type which contains only one of its member object at given time. since relxloc relyloc and color mapped to the same memory location, hence the output.


More precisely, it's a defined type which contains enough raw data to represent any single one of the "members" it contains, and which may be interpreted as any of those types via a member selection. Having multiple "members" of the same type in a union is unlikely to ever be useful (unless you really want to support alternate names for the exact same thing).

Quote:
To be perfectly honest, I have never ever used a union, and I can see very few cases where they would be both really useful and easy to understand


Usually they are used to make it easier to understand various low-level byte hacks that would be more difficult to understand otherwise, or to provide alternate names for things. E.g.
union quad {  int asASingleValue;  char asBytes[4];  struct asStruct {    char r,g,b,a;  };}void reverseBytes(quad& q) {  swap(qq.asBytes[0], qq.asBytes[3]);  swap(qq.asBytes[1], qq.asBytes[2]);}void doGammaCorrection(quad& q) {  // similarly, make use of q.asStruct.a-d}void doMathWith(quad& q) {  // similarly, make use of q.asASingleValue}// Of course, these look like "member functions"; you might prefer to wrap the// union in a struct or class so that you can provide them with that syntax:class quadWrapper {  quad q;  public:  // I think this will work, and still let the class be treated as a POD type...  quadWrapper(int x) : q.asASingleValue(x) {}  // similar ctors taking a char[4] or four separate chars  void reverseBytes() {     swap(q.asBytes[0], q.asBytes[3]);    swap(q.asBytes[1], q.asBytes[2]);  }  // etc.}


(Disclaimer: Last time I actually used a union, it was in plain old C.)
Robot Guy
Robot Guy
By the way, never ever use constructs like rand()%10 to generate small random numbers. The way most C librarys implement rand the lower few bits at not very random. I once helped somebody with a coin toss program whose results were consistantly Heads, Tails, Heads, Tails, Heads, Tails, etc as they had used rand()%2.

Use this instead (taken from Numerical Recipes in C) :
(int)(10.0*rand()/(RAND_MAX+1.0))
lucky_monkey
lucky_monkey
This web page has a nice looking pseudo-random number generator...
I haven't used it yet, but at some point I'll probably plug it into a little program I wrote a while ago to test the randomness of rand().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.