CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Dec 2003
    Posts
    112

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured