Help with Randomization of Non-Overlapping Images

David McFarlane mcfarla9 at msu.edu
Mon Aug 4 19:01:38 UTC 2014


Tara,

Wow, that Loop While statement just hurts my eyes!  When something 
gets that complicated, I would at least try to format it better for 
readability (indentation and the "_" continuation character are your 
friends here), or better yet find some way to restructure the code so 
as to avoid such a long expression.  E.g., if you used some arrays 
instead of simple variables then you could use a loop or two to run 
through all those tests that appear explicitly in your While expression.

So I do not know if something goes wrong with your code per se.  But 
your approach here reminds me of a bogosort (see 
http://en.wikipedia.org/wiki/Bogosort ), and it may suffer from the 
same problem.  Even with no errors in the code, the code must find a 
solution *by chance*, and *by chance* that may take many tries.  That 
may work well enough for small problems, but as the proportion of 
solutions in the problem space gets smaller this approach takes 
longer and longer to find one.  In particular, perhaps for 3 
coordinates your "bogosort" code could quickly find a solution, but 
for 4 images it just takes a lot longer to find one *by 
chance*.  (PST's own "No Repeats on Consecutive Trials" examples, 
download from their website, suffer the same problem.)

I do not like algorithms that work by chance, so I avoid them 
whenever possible.  So first let's restate your problem, and see what 
we can do.

You want to find 4 unique, randomly chosen x values from a pool of 7 
values, and 4 unique, randomly chosen y values from a pool of 23 
values.  IOW, you want to pick 4 x values randomly without 
replacement from the range [36, 42], and 4 y values randomly without 
replacement from the range [38, 62].  Did I get that right?

Ideally, you would follow David V's advice, make a nested List with 
all possible (x, y) pairs and just pick from that (Random, without 
replacement).  That should involve 7 x 23 = 161 rows, which would not 
be too much trouble, and Excel would make it even easier to construct 
such a List.  Alternatively, you might use two nested Lists, one for 
all the x values and another for all the y values, each Random 
(without replacement) and pick from those.

But if you like inline code, I might start by making a couple arrays 
to hold my pools of x & y values:

     Const xPoolSize as Integer = 7, xPool1 as Integer = 36
     Const yPoolSize as Integer = 23, yPool1 as Integer = 38
     Dim i as Integer
     Dim xPool( 1 to xPoolSize ) as Integer
     Dim yPool( 1 to yPoolSize ) as Integer
     xPool(1) = xPool1
     For i = 2 to xPoolSize
         xPool(i) = xPool(i-1) + 1
     Next i
     yPool(1) = yPool1
     For i = 2 to yPoolSize
         yPool(i) = yPool(i-1) + 1
     Next i

With a little trouble you could compact that code even better by 
using a 2-dimensional array for x & y together, although then the 
next step would not work.  I leave that as an exercise.

Next, just shuffle these arrays.  VBA/E-Basic provides a command just 
for this (but only for 1-dimensional arrays, sigh):

     RandomizeArray xPool
     RandomizeArray yPool


You may find RandomizeArray documented in the E-Basic Help facility.

Now you may simply pick the first 4 values from the xPool & yPool 
arrays to construct your coordinates.

Does that look like it will work?  What have I missed?

-----
David McFarlane
E-Prime training 
online:  http://psychology.msu.edu/Workshops_Courses/eprime.aspx
Twitter:  @EPrimeMaster (https://twitter.com/EPrimeMaster)

/----
Stock reminder:  1) I do not work for PST.  2) You may reach PST's 
trained staff (and other support facilities) at 
https://support.pstnet.com .  3) If you do get an answer from PST 
staff, please extend the courtesy of posting their reply back here 
for the sake of others.
\----


At 8/1/2014 05:43 PM Friday, tamiskovich at gmail.com wrote:
>Thanks for your response David. That is what I was afraid of. Eprime 
>support said it should work in theory, but seems as though it keeps 
>getting stuck when I add in the forth image. I am trying to list all 
>the combinations as well, it is just proving to be a huge task, for 
>there are an enormous amount of possible combinations.
>
>I was hoping that this would make coding much quicker and truly random.
>
>Tara
>
>On Friday, August 1, 2014 12:54:35 PM UTC-5, Vinson, David: UCL wrote:
>I suspect you didn't get any replies before due the impressively 
>complicated set of OR statements in that loop. I suspect the problem 
>is there somewhere but imagine it world be a nightmare to track it 
>down. My guess is the loop never exits, whether due to an error or 
>impossible constraints.
>
>What if instead you enumerated the possible legal combinations of 
>positions in a nested List and then just sampled one of them per trial?
>
>Good luck,
>David V
>
>----- Reply message -----
>From: "tamis... at gmail.com" <tamis... at gmail.com>
>To: "e-p... at googlegroups.com" <e-p... at googlegroups.com>
>Subject: Help with Randomization of Non-Overlapping Images
>Date: Fri, Aug 1, 2014 6:30 pm
>
>
>
>Does anyone have any suggestions on this? I'm stuck on this and 
>eprime support directed me here.
>
>Tara
>
>On Monday, July 28, 2014 3:43:33 PM UTC-5, Tara Miskovich wrote:
>Hello,
>
>I am working on having 4 images randomly places within a rectangular 
>region without overlapping. I wrote the code below, and it appears 
>to be working with up to Image 3 added, but when I add in the forth 
>image the program freezes. I am hoping for some guidance with what 
>to try next or how to get past this. Here is the code I wrote to 
>randomize the x and y location for each image.
>
>Thanks!
>Tara
>
>Dim x1 As Integer
>Dim x2 As Integer
>Dim x3 As Integer
>Dim x4 As Integer
>Dim y1 As Integer
>Dim y2 As Integer
>Dim y3 As Integer
>Dim y4 As Integer
>
>x1 = random(36, 42)
>y1 = random(38, 60)
>
>Do
>x2 = random(36, 42)
>y2 = random(38, 60)
>x3 = random(36, 42)
>y3 = random(38, 60)
>x4 = random(36, 42)
>y4 = random(38, 60)
>
>
>Loop While (x2 = x1 Or x2 = x1 + 1 Or x2 = x1+2 Or x2 = x1 - 1 Or x2 
>= x1 - 2 _
>And y2 = y1 Or y2 = y1 + 1 Or y2 = y1 + 2 Or y2 = y1 + 3 Or y2 = y1 
>- 1 Or y2 = y1 - 2 Or y2 = y1 - 3)_
>Or (x3 = x1 Or x3 = x1 + 1 Or x3 = x1+2 Or x3 = x1 - 1 Or x3 = x1 - 2 _
>And y3 = y1 Or y3 = y1 + 1 Or y3 = y1 + 2 Or y3 = y1 + 3 Or y3 = y1 
>- 1 Or y3 = y1 - 2 Or y3 = y1 - 3)_
>Or (x3 = x2 Or x3 = x2 + 1 Or x3 = x2+2 Or x3 = x2 - 1 Or x3 = x2 - 2 _
>And y3 = y2 Or y3 = y2 + 1 Or y3 = y2 + 2 Or y3 = y2 + 3 Or y3 = y2 
>- 1 Or y3 = y2 - 2 Or y3 = y2 - 3)_
>or (x4 = x1 Or x4 = x1 + 1 Or x4 = x1+2 Or x4 = x1 - 1 Or x4 = x1 - 2 _
>And y4 = y1 Or y4 = y1 + 1 Or y4 = y1 + 2 Or y4 = y1 + 3 Or y4 = y1 
>- 1 Or y4 = y1 - 2 Or y4 = y1 - 3)_
>Or (x4 = x2 Or x4 = x2 + 1 Or x4 = x2 + 2 Or x4 = x2 - 1 Or x4 = x2 - 2 _
>And y4 = y2 Or y4 = y2 + 1 Or y4 = y2 + 2 Or y4 = y2 + 3 Or y4 = y2 
>- 1 Or y4 = y2 - 2 Or y4 = y2 - 3)_
>Or (x4 = x3 Or x4 = x3 + 1 Or x4 = x3 + 2 Or x4 = x3 - 1 Or x4 = x3 - 2 _
>And y4 = y3 Or y4 = y3 + 1 Or y4 = y3 + 2 Or y4 = y3 + 3 Or y4 = y3 
>- 1 Or y4 = y3 - 2 Or y4 = y3 - 3)
>
>c.SetAttrib "x1", x1
>c.SetAttrib "x2", x2
>c.SetAttrib "x3", x3
>c.SetAttrib "x4", x4
>c.SetAttrib "y1", y1
>c.SetAttrib "y2", y2
>c.SetAttrib "y3", y3
>c.SetAttrib "y4", y4

-- 
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To unsubscribe from this group and stop receiving emails from it, send an email to e-prime+unsubscribe at googlegroups.com.
To post to this group, send email to e-prime at googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/e-prime/53dfd89f.066b320a.6adf.5935SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.



More information about the Eprime mailing list