Using rand() to generate a random point in a 2d matrix


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Using rand() to generate a random point in a 2d matrix

  1. #1
    Join Date
    May 2005
    Posts
    33

    Arrow Using rand() to generate a random point in a 2d matrix

    Location Matrix::randomAdjacentLocation(Location& location)
    { int row = location.getRow();
    int col = location.getCol();
    .
    .
    }
    Above ‘location’ represents an occupied point in a two-dimensional matrix. How can I use row, col and rand() to generate a (random) point that is adjacent to ‘location’? If all adjacent points to ‘location’ are occupied, how can I get the function to look further afield, that is, how can I increment row and col non-uniformly? I would be very grateful for all help.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    0 1 2
    3 4
    5 6 7

    there are 8 locations adjacent to your element as seen here (if the formatting doesnt mess me up). So, choose a number from 0-7 by rand()%8 (honestly, get a better random number generator if this is an important project) and then move to that row/col pair (this should be trivial for you? if not ask). Make sure the location is legal (you were not on the edge or corner etc) and if it is not legal, generate random locations until it IS legal (at the worst case your in a corner, with only 3 locations to choose from, but a good random generator will find a legal one in a few iterations at most 5 or 10).

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    As for uniform or non uniform, that is totally up to the random generator you select. You can craft one to do a bell curve or whatever statistical distribution you need.

    To look farther away, run the algorithm twice (move to occupied square, run again) until you find an empty one, this should be as random as your generator will allow.

  4. #4
    Join Date
    May 2005
    Posts
    33
    Quote Originally Posted by jonnin
    0 1 2
    3 4
    5 6 7

    there are 8 locations adjacent to your element as seen here (if the formatting doesnt mess me up). So, choose a number from 0-7 by rand()%8 (honestly, get a better random number generator if this is an important project) and then move to that row/col pair (this should be trivial for you? if not ask). Make sure the location is legal (you were not on the edge or corner etc) and if it is not legal, generate random locations until it IS legal (at the worst case your in a corner, with only 3 locations to choose from, but a good random generator will find a legal one in a few iterations at most 5 or 10).
    Many thanks for your reply, I have put it into practice and it works. I wonder if you could suggest where I can look for a better random number generator.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    I really don't know -- I wrote my own and tested it until it routinely passed the DIEHARD tests (do a google search, it tests the "randomness" of a stream and is a good measure of how good your generator is). I can post that one later today. Or look online for some (maybe boost.org) -- I think knuth wrote a public domain one as well.

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    Here is the one I wrote. It started life in "c" so you will probably want to poke at the code style -- anyway, the numbers should be statistically un-related to a high degree, and the math is what matters.

    #ifndef jrand
    #define jrand

    #include<math.h>
    #include<stdlib.h>
    #include<stdio.h>

    static unsigned int seed = 1; /*global & always exists*/

    /*headers*/
    void setrand(unsigned int s); /*set the seed*/
    unsigned int jrandi(); /*random(uniform) unsigned int*/

    #endif

    #include"jrand.h"

    void setrand(unsigned int s)
    {
    seed = s;
    }

    unsigned int jrandi()
    {
    int v;
    int u = (seed*16807)%2147483647 +seed%5;

    /*simple random numbers,*/

    int t = seed++ << 7;

    t += u;


    //v = ((int)((u*sin(u)+1)*(u/3)));
    v = (int)(sin(u)*10000);
    v++;
    v *= u;
    v *= u/3;
    v /= 10000;
    t ^= v;

    v = t;

    u = (seed*16807)%2147483647 +seed%5;

    t = seed++ << 7;
    t += u;

    v = (int)(sin(u)*10000);
    v++;
    v *= u;
    v *= u/3;
    v /= 10000;
    t ^= v;

    v = (v >> 16);
    t+=v;
    seed += t;

    return((unsigned int)(t));
    }

Similar Threads

  1. help with sparse matrix
    By jrhaley123 in forum Java
    Replies: 8
    Last Post: 09-28-2013, 11:48 PM
  2. Replies: 1
    Last Post: 09-01-2002, 09:52 AM
  3. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 04:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center