Creating random points

321 views
Skip to first unread message

B

unread,
Jul 27, 2009, 8:16:37 PM7/27/09
to MapInfo-L
I am new to mapbasic coding so if this is easy to do i apoloigse in
advance :)

I am trying to write some code so that a series of random points can
be generated with a user defined area. (may not always be a a simple
rectangle/circle shape)

The process i am trying to create is; 1. user draws in area of
interest, 2. user defines number of random points they want, 3. output
= a tab file created with the number of random points as defined by
the user. Table structure to be ID (rowid) and then the X and Y co-
ords.

look forward to hearing suggestions on how to code this

cheers
ben

Timothy Mashford

unread,
Jul 27, 2009, 11:21:51 PM7/27/09
to mapi...@googlegroups.com
Hi Ben, this sounds like a good project for learning MapBasic.

1. Create a ToolButton where the draw mode = DM_CUSTOM_POLYGON
2. Read the drawn polygon into an object variable (and also insert it into the cosmetic layer if you wish)
3. Create a dialog which asks for the number of points required, and create an empty table to store the points
4. Get the MBR of the polygon, as your extreme coordinates (xmin, xmax, ymin, ymax)
5. Use Randomize and Rnd(1) to create X and Y coordinate within the extremes, create a point into an object variable
6. Check if the point variable is WITHIN the polygon variable; if it is, insert it into your point table and update a 'success' counter
7. Repeat steps 5-6 as many times as you need to until you have the number of points required (WHILE loop)

You might want to Set Coordsys so you're dealing with the coordinate types you want.

Tim

B

unread,
Jul 28, 2009, 8:24:03 PM7/28/09
to MapInfo-L
Tim,

thanks for the response, but i am REALLY new at this so can i ask you
break it down a bit more for me?

step 2 - when the user clicks on the button and draws the shape, how
do i load it into the cosmetic layer? or store it to a varibale, i
tried the 'into' clause but it returns errors?

step 3, 6 and 7 i can do, 4 and 5 i am bit unclear on so any examples
of code would be useful?

Timothy Mashford

unread,
Jul 29, 2009, 2:59:23 AM7/29/09
to mapi...@googlegroups.com
Hi Ben, make sure you use the help file as much as possible, it's the best way to learn. Some of the stuff is a bit hidden, but if you do keyword searches you'll eventually find what you're looking for.

Step 2: obj_variable = CommandInfo(CMD_INFO_CUSTOM_OBJ)    (see 'Alter ButtonPad')

Step 4: Look up MBR(), and ObjectGeography()

Step 5: To generate random numbers, first initiate with:  Randomize With Timer()

Then, use Rnd(1) which returns a float number between 0 and 1. Use this to create an X coordinate between your xmin and xmax coords (and similar for Y coordinate) e.g.

xnew = ((xmax-xmin)*Rnd(1)) + xmin

Tim

Uffe Kousgaard

unread,
Jul 31, 2009, 7:43:21 AM7/31/09
to mapi...@googlegroups.com
I just realized I created some code for creating random points several
years ago:
http://www.routeware.dk/download/randompoints.zip

Included in the zip file are some screen dumps that illustrate mapbasic
had a lousy random number generator. A test shows it has not been
improved in 9.5.1.

Regards
Uffe Kousgaard

Bill Thoen

unread,
Jul 31, 2009, 11:49:05 AM7/31/09
to mapi...@googlegroups.com
Uffe Kousgaard wrote:
> I just realized I created some code for creating random points several
> years ago:
> http://www.routeware.dk/download/randompoints.zip
>
> Included in the zip file are some screen dumps that illustrate mapbasic
> had a lousy random number generator. A test shows it has not been
> improved in 9.5.1.
>

That's because they use the standard C library's fast but simple
psuedo-random number generator (probably Lehmer's Linear Congruential
method). AFAICT all the commercial mapping and GIS packages use this
algorithm for their random number generation because it's fast, but it's
got a pronounced periodic linear bias so it's definitely not "random"
enough for a random number generator that is likely to be used in a
spatial application. Knuth's method (described in Sedgwick's classic
"Algorithms" book is much better. See RANARRAY at
http://www-cs-faculty.stanford.edu/~knuth/programs.html for the details,
or Google for "knuth random number".

- Bill Thoen

David Reid

unread,
Jul 31, 2009, 6:45:34 PM7/31/09
to mapi...@googlegroups.com

Bill: "probably Lehmer's Linear Congruential method"

Peanut the ventriloquist actor: "Gesundheit"


Bill: "This algorithm has a pronounced periodic linear bias"

Curly of the Three Stooges:
"Maybias we should, have a dentiast have a lookias at this bias"


Bill: "Knuth's method (described in Sedgwick's classic "Algorithms" book is
much better.

Radar O'Reily from M*A*S*H: "Aaaaah 'BACH'"!

Bill Thoen

unread,
Jul 31, 2009, 11:29:55 PM7/31/09
to mapi...@googlegroups.com
You soundin' like you ain't never seen more'n two twenty-dollar words
all t'gether on the same page before... Good ta know they's others'n
this ol' world what didn't let they schoolin' interfere with they
edjucashun!

Uffe Kousgaard

unread,
Aug 1, 2009, 2:53:38 AM8/1/09
to mapi...@googlegroups.com
Hi Bill,

The only fast thing about that function must for the developers within
PB. I looked up your RANARRAY and it doesn't look especially simple or
anything. Have a look at this one:

RS must be an unsigned 64-bit integer

// calculates a random number 0 <= x < 4 GB
RS = RS * $08088405 + 1
RS = RS mod $100000000

This passes most PRNG tests you can think of and has been known for
decades. Since it uses 64-bit integers, you can't implement it in
mapbasic on your own, but need a DLL.

Regards
Uffe Kousgaard

-------- Original Message --------
Subject: [MI-L] Re: Creating random points
Reply all
Reply to author
Forward
0 new messages