• Advertisement
Sign in to follow this  

A quick question concerning logic

This topic is 3232 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
Quote:
Original post by jonathanc
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 :(

I believe [ source ] tags are used for that.

And if you are only using a variable once, then you don't want to declare it static. Static means the variable is created once at the beginning of runtime, and maintains it's state between function calls. So if you only plan to use that function(and hence that variable) once, declaring it static actually wastes the memory of that variable by maintaining it even though you aren't going to use it again. If you just declare it like a normal variable, it will be de-allocated when you leave the scope of the function, freeing that space.

Share this post


Link to post
Share on other sites
Quote:
Original post by jonathanc
I guess its a bad practice but I tend to use static to declare the temporary variables


That is pretty much the exact opposite of what 'static' means in that context. 'static' here means "keeps a permanent place in memory, so that the value is remembered and reused between calls to the function".

Quote:
So instead of having to declare count (which I will probably just use once) I can just use static.


The line where you "use static" is a declaration. To "declare" count, you write less code (because you write everything that you would the 'static' way, except 'static' itself), and you can use the variable as many times as you like (including "just once").


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


In that code, "count" would only be set to 0 the first time you call Foo(). Every other time, it would use whatever value it ended up having after the last time Foo() was called. The name "count" refers to a specific chunk of memory that is set aside ahead of time for Foo() to use.

If you just write 'int count = 0;', the variable is set to 0 every time you call the function. The name "count" refers to a chunk of memory that is newly acquired (from "the stack") each time Foo() is called, and released (along with everything else that's "stack-allocated") when Foo() returns. (Of course, that's only if the compiler doesn't make any optimizations. It might not even actually call Foo() when you tell it to, but simply create the same net effect.)

Anyway, if you are only going to use the variable once, however, you might not need a variable at all:


int triple(int x) {
// NOT REQUIRED:
// int result = 3 * x;
// return result;
return 3 * x;
}


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


A) You can click "edit" on people's posts to see exactly what people wrote for their post. (The system will simply reject any edits that you try to save.)

B) There is a link at the top of every page on the forum - including the posting form - labelled "faq", with an icon of a life preserver and magic wand.

Share this post


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

  • Advertisement