|
-
October 19th, 2007, 11:28 AM
#1
2D Random Numbers
I am trying to create a seeded random number generator that generates a number based on coordinates.
Code:
int rand(int x, int y);
Basically, given the same seed, every time this function is called, it will give the same random number for the same coordinates.
What I am really aiming for is some sort of seeded Noise function that will accept any X and Y values.
Some requirements.
Ideally, resulting values would be either between 0 and RAND_MAX, then I can manipulate as required.
(-1,1) and (1,-1) must return different values.
I had thought to do something like the following:
Code:
int rand(int x, int y)
{
srand(x + seed);// Seed based on X
srand(y + rand());// Seed based on Y and the results of X
return rand();// Return Random value
}
Now this does work, however I noticed some weirdness. The resulting values actually seem to be clustered. So if I use (1, 1), (-1, -1), (-1, 1), (1, -1), each returns a different value, however all values are within a small range (eg. 5000, 5010, 4996, 4985). I would expect to see each value being significantly different.
Does anyone have another suggestion for how this could be accomplished? Something simpler? Is this clustering effect a flaw in the C++ rand/srand? I did notice that if I simply call srand(seed), and rand(), the resulting random number seems to get higher as the seed gets higher. That doesn't seem very random to me.
Last edited by Eggman002; October 19th, 2007 at 11:31 AM.
Eggman
Using: VS 2008 w. Net 3.5
-
October 19th, 2007, 11:56 AM
#2
Re: 2D Random Numbers
Boost has a powerful set of random number generation functions that you could check out.
http://www.boost.org/libs/random/index.html
"Live only for tomorrow, and you will have a lot of empty yesterdays today."
-
October 19th, 2007, 12:23 PM
#3
Re: 2D Random Numbers
Thanks, but I am looking for how to create my own method. I would prefer to avoid using anything outside the standard libs (for portability).
Also, couldn't see anything in there for 2D like I am wanting.
Eggman
Using: VS 2008 w. Net 3.5
-
October 20th, 2007, 02:11 PM
#4
Re: 2D Random Numbers
 Originally Posted by Eggman002
Does anyone have another suggestion for how this could be accomplished? Something simpler? Is this clustering effect a flaw in the C++ rand/srand? I did notice that if I simply call srand(seed), and rand(), the resulting random number seems to get higher as the seed gets higher. That doesn't seem very random to me.
It doesn't make much sense to constantly re-seed the random generator. All you are using in your app is a *FIRST* member of pseudo-random sequence.
My understanding is that each sequence consists of pseudo-random numbers, but no claims are made about the first member and its relation to the seed.
In a typical application, you seed the generator once on a start-up, usually with time(NULL).
If you want any correlation with the input pair – what’s random about that?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
October 26th, 2007, 11:43 AM
#5
Re: 2D Random Numbers
Further thought on this matter has led me to realize that what I actually want is a way to take a set of coordinates, and create a unique integer that corresponds to those coordinates. That integer would then be used to seed the random number generator, which would then be used for a variety of purposes.
So I need a way to take X,Y and convert it to a unique integer using a function we will name getseed.
Rules:
getseed(X1,Y1) != getseed(X2, Y2)
getseed(-X1,-Y1) != getseed(X1,Y1)
So the idea is that each set of coordinates would have a different random number generator. That random number generator would be the same every time you use the same coordinates, but it would be different at each other set of coordinates.
Last edited by Eggman002; October 26th, 2007 at 12:34 PM.
Eggman
Using: VS 2008 w. Net 3.5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|