Don't repeat coordinates

BagzliBagzli Member, PRO Posts: 48

Hello,

I am trying to figure out a way where I can 100% guarantee to never random the same set of coordinates. I am spawning an actor and telling him to move to a random point on the map. The formula is simple:

If All are True
If self.position.x = self.Destination_X,
If self.position.y = self.Destination_Y
Self.Destination_X = (5 * 50 + 250) ---Where 5 is the random number between 1 and 25 and 50 is the spacing of points on the map and 250 is the indentation from the left to ensure the actor is within the bounds.
Self.Destination_Y = (6 * 50 + 250)
Move To
Self.Destination_X, Self.Destination_Y At Speed 100

Quite straight forward, the problem though is that I need to make sure that the 5 and 5 don't random in pair together. I know I can random a number between 1 and 25 and make sure that the number is not 5, but I cannot figure out how to do this in combination and at the same time prevent what if it happens few times in a row (Less Likely but I wish to ensure no bugs occur due to this)

Again with example above, I am trying to make sure that once I random the numbers, I don't get 5 and 6 which would cause the actors not to move at all.

Any help is most appreciated!

Comments

  • tintrantintran Member Posts: 453

    make your 5 into an attribute call it random_x (for example)
    initialize random_x = random(1,25)
    then each time you want a different random_x just change attribute using
    random_x = mod(random_x + random(0,23),25) + 1

    that way your random_x will always be a different number from 1 and 25 but different from what it was all the time

  • BagzliBagzli Member, PRO Posts: 48

    yes but that means that my guy can never go in a straight line, and I was trying to avoid that.

  • tintrantintran Member Posts: 453
    edited July 2015

    @Bagzli said:
    yes but that means that my guy can never go in a straight line, and I was trying to avoid that.

    Currently you have it he moves toward a destination point. then randomly chooses a new destination right ..not all destinations are on a straight line right?
    I don't see what you have currently that guarantees that it's a straight line.
    Then you recalculate random_x, then he still moves toward that new destination in a straight line using your currently move to command.
    What i mean is only set random_x when he reaches the destination...so that he still moves toward destination in a straight line
    so that you have something like this

    Self.Destination_X = (random_x * 50 + 250)

    you mentioned that your 5 is random number between 1 and 25 so it's no different from what you have now, you'll just use that formula when you want a new destination, it'll give you a different value from 1 to 25 but different from your current value.


    or by straight line do you mean sometimes you want him to travel to a different X and staying on the same Y or vice versa? like horizontal or vertical moves.

    if that's the case you can use something like
    vertical = random(0,1)
    random_x = mod(random_x + random(0,23+vertical,25) + 1
    random_y = mod(random_y + random(0,23+abs(vertical-1)),25) + 1

    then set your
    Self.Destination_X = (random_x * 50 + 250)
    Self.Destination_Y = (random_y * 50 + 250)

    this way when vertical is 0, you get a different random_x and a random_y that could be unchanging
    and when vertical is 1, you can get an unchanging random_x and a different random_y
    but together they'll always give you a different destination.

  • BagzliBagzli Member, PRO Posts: 48

    Yes to your last sentence, but I think I figured out a way. If x and y are same as before, then choose x and use this formula to force x to be something else. So in situations where I get the same roll twice, I will force x to change. This shouldn't happen too often with over 500 options of coordinates. So I got an answer now, thanks :)

Sign In or Register to comment.