0

Basically I am creating a grid and plotting points on it, and no two points can be on the exact same place [(3,4) is different than (4,3)]. The y coordinates have to be within 2 and 7 (so 2, 3, 4, 5, 6, 7) and x coordinates have to be within 1 and 7. I have a getRandom function (which can be seen below) which generates a random number between a min and max range. Here is what I have so far.

var xposition = []; var yposition = []; var yShouldBeDifferentThan = [] function placeRandom() { for (s=0; s<xposition.length ; s++ ) { if (xposition[s] == x) { // loops through all numbers in xposition and sees if the generated x is similar to an existing x yShouldBeDifferentThan.push(yposition[s]); //puts the corresponding y coordinate into an array. for (r=0; r<yShouldBeDifferentThan.length; r++) { while (y == yShouldBeDifferentThan[r]) { y = getRandom(2,7); } } } } xposition.push(x); yposition.push(y); } 

The problem with this is, if

xposition = [1, 5, 5, 7, 5, 5] yposition = [1, 3, 7, 2, 3, 6] yShouldBeDifferentThan = [3, 7, 3, 6] 

First, it will generate a random number different thah 3, say 6. Then (I think) it will see: 6 == 7? It doesn't. 6 == 3? It doesn't. 6 == 6? It does, so generate a random number different than 6. This is where the problem comes, it might generate the number 3. My getRandom function is the following:

function getRandom(min, max) { return min + Math.floor(Math.random() * (max - min + 1)); } 

I was thinking making the getRandom function such that I can exclude numbers as well if I want, but I don't know how to do this. If I can get it to exclude numbers, than in the last while loop of the placeRandom function, maybe I can do something like:

y = getRandom(2,7) // excluding all numbers which already exist in the ShouldBeDifferentThan array 

Also, note that I cannot use the indexOf method since I am using Internet Explorer 8.

2 Answers 2

2

There are two problems with your approach:

  • You might pick an x coordinate for a row that is already full, which would send the code into an eternal loop.

  • Picking an x coordinate and then a y coordinate means that positions will have a different chance to be picked depending on how many positions were picked in the same row before.

Instead just pick an x and y coordinate, and check if that specific coordinate was picked before. If it was, start over.

function placeRandom() { do { var x = getRandom(2,7), y = getRandom(2,7), found = false; for (s = 0; s<xposition.length; s++) { if (xposition[s] == x && yposition[s] == y) { found = true; break; } } } while(found); xposition.push(x); yposition.push(y); } 

Additionaly, when the grid starts to get full (e.g. around 80%), you can make an array containing all the remaining positions and pick one by random from that.

Sign up to request clarification or add additional context in comments.

5 Comments

wait, "if (xposition[s] == x && yposition[s] == y)" it means that there already exists a point with the same x and y coordinate, right? So how come you break and then push those x and y coordinates into xposition and yposition? Shouldn't you generate a different number randomly for x and y and then recheck "if (xposition[s] == x && yposition[s] == y)" and repeat until if (xposition[s] == x && yposition[s] == y)" isn't true?
@user2719875: The break is to get out of the for look checking for the coordinates, so that it doesn't have to check the rest of the coordinates in the array. The do {} while loop makes it pick new coordinates until no match is found. The code works without the break too, but then it does some work that it doesn't have to.
right, but if found = true then it means that (xposition[s] == x && yposition[s] == y) right? So don't we Not want (xposition[s] == x && yposition[s] == y)? How come we are breaking and pushing x and y into xposition and yposition if they already exist? Should it be "if (!(xposition[s] == x && yposition[s] == y))"?
@user2719875: When we find an already used square, the found variable is set to true, and the while loop continues. It's only when the coordinates is not found anywhere in the array that the found variable remains false, we exit the while loop and add the coordinates to the array.
Oh, okay that makes sense. I tried this and it works perfectly. Thanks.
1
var numbers = [ 1, 2, 3, 4, 5 ]; var exclude = [ 3, 4 ]; var filtered = []; for (var i = 0; i < numbers.length; i += 1) { if (exclude.indexOf(numbers[i]) === -1) { filtered.push(numbers[i]); } } var rand = Math.floor(Math.random() * filtered.length); var num = filtered[rand]; // 1, 2 or 5 

Build the list of allowed numbers, pick one of those at random. The for-loop is just a diff between numbers and exclude, like: var filtered = numbers.diff(exclude);

2 Comments

hm, I can't use indexOf since I am using I.E 8. Sorry I didn't mention that earlier.. Is there any way to do it without using indexOf?
Make your own diff function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.