
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 twodimensional 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 nonuniformly? I would be very grateful for all help.

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 07 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).

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.

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 07 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.

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.

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 unrelated 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

By jrhaley123 in forum Java
Replies: 8
Last Post: 09282013, 10:48 PM

By Ad van Klink in forum .NET
Replies: 1
Last Post: 09012002, 08:52 AM

By ASPSmith Training in forum dotnet.announcements
Replies: 0
Last Post: 06182002, 03: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

Forum Rules

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