Jump to content
  • Advertisement
Sign in to follow this  
jonathanc

A quick question concerning logic

This topic is 3469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am trying to create a set of 2D random numbers that range from -1 to 1. The values can be as follows: x | y 0 | 1 0 | -1 1 | 0 -1 | 0 Right now I have:
	int X = (rand()%3 - 1); // range from -1 to 1
        int Y = (rand()%3 - 1);
This sort of works but it will still give unwanted values like (0,0), (-1,-1) etc. Is there anyway to overcome this problem? I tried using a few if statements and it will always give me a same set of results if I generate a lot of iteration of (x.y). I want to generate a few iterations of that 2d number with the given condition. Hope someone could give some pointers. Thanks! Please don't mark threads 'solved' in this forum. -- jpetrie [Edited by - jpetrie on April 20, 2009 10:25:51 AM]

Share this post


Link to post
Share on other sites
Advertisement
You could just generate a number from 0 to 3 and use that to choose between (0, 1), (0, -1), (1, 0) and (-1, 0).

Share this post


Link to post
Share on other sites
Hmmm, noticed that every iteration generates the same numbers :S Can anyone please see if there's anything wrong with this code?


void random(int i)
{
static int X = (rand()%3 - 1); // range from -1 to 1
static int Y = (rand()%3 - 1);

if (tempXX == tempYY) tempXX = (rand()%3-1);
pos.x = X;
pos.y = Y;
printf("Seed no : %d pos.x value = %d pos.y value = %d\n\n", i, pos.x, pos.y);
}

for(int i=1; i<10; i++)
{
random(i);
}


Now everytime I ran random I will get 10 sets of the same numbers. Is this just the law of probability? o.0 I seeded the random number generator with the current system time as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You could just generate a number from 0 to 3 and use that to choose between (0, 1), (0, -1), (1, 0) and (-1, 0).


Hmm, I am sorry but I am not so sure what you mean by that. Could you kindly explain please?

edit:

Ahh I think I know what you mean. Thanks, will try that out.

Share this post


Link to post
Share on other sites
If the random number is 0, then your point is (0, 1). If it's 1, then the point is (0, -1). If it's 2, then (1, 0) and if it's 3, then (-1, 0).

Share this post


Link to post
Share on other sites
Hmm, can anyone please tell me whats wrong with this code? It keeps generating the same set of numbers:

void random(int i)
{
static int tempX = ((MaxX-3) - (MinX+3));
static int tempZ = ((MaxZ-3) - (MinZ+3));
static int finalX = tempX;
static int finalZ = tempZ;
position.x = rand()%finalX - MaxX;
position.y = 0;
position.z = rand()%finalZ - MaxZ;

static int rand1 = rand()%4;
if (rand1 == 0)
{
orient.x = 0;
orient.z = 1;
}
else if (rand1 == 1)
{
orient.x = 0;
orient.z = -1;
}
else if (rand1 == 2)
{
orient.x = 1;
orient.z = 0;
}
else if (rand1 == 3)
{
orient.x = -1;
orient.z = 0;
}
printf("Seed no : %d orient.x = %d orient.z value = %d\n\n pos.x : %d, pos.z: %d", i, orient.x, orient.z);
}

for(int i=1; i<10; i++)
{
random(i);
}


When I run the iterations, the positions gives nice random numbers but I will get the same set for orient values...

Share this post


Link to post
Share on other sites
Meh, I think I solved it myself :P Must be tired after programming for so many hours. It's the static int thats doing it. This code should work:



void random()
{
static int rand1 = 0;
rand1 = rand()%4;
if (rand1 == 0)
{
orient.x = 0;
orient.z = 1;
}
else if (rand1 == 1)
{
orient.x = 0;
orient.z = -1;
}
else if (rand1 == 2)
{
orient.x = 1;
orient.z = 0;
}
else if (rand1 == 3)
{
orient.x = -1;
orient.z = 0;
}


Cheers for the pointers anyway! Thanks!

Share this post


Link to post
Share on other sites
Why are you making the variable "rand1" static? Can you tell me what you think "static" does, and what benefit you get from using it in your function?

Here is a simpler solution. Create an array of the acceptable return values and return a random element of the array:

struct Point // or whatever describes the object
{
int x, y;
};

Point random()
{
static const Point points [] =
{
{ 0, 1},
{ 0,-1},
{ 1, 0},
{-1, 0},
};
return points[rand() % 4];
}

Share this post


Link to post
Share on other sites
Quote:

Why are you making the variable "rand1" static? Can you tell me what you think "static" does, and what benefit you get from using it in your function?

Here is a simpler solution. Create an array of the acceptable return values and return a random element of the array:


Hi!

Thanks for your suggestion. It's indeed a more elegant solution. However, in my case I will not be repeating the task other then to retrieve those four random points. I guess its a bad practice but I tend to use static to declare the temporary variables that I will be using and initializing within a function. For example :


int Foo()
{
static int count = 0;
if(!Test)count++
if(count == 0) return 0;
}


So instead of having to declare count (which I will probably just use once) I can just use static. I will read up more on static and I guess you learn from mistakes! Thanks for pointing that out though.

p/s: How did you do the code box? I'm stuck with tags :(

Share this post


Link to post
Share on other sites
You can use the [source] tags to get the white code box.
Quote:
Original post by jonathanc
So instead of having to declare count (which I will probably just use once) I can just use static.

That makes no sense.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!