Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Random but no duplicates?

    Beginners
    5
    6
    415
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SteveZX81
      SteveZX81 F last edited by

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • PB____
        PB____ last edited by

        I don't think this is an elegant solution at all, and I haven't tested it. (I'm a bit tired but couldn't help having a throw at it)
        But I think something like this should work:

        function place(position)
        	// your placing logic, where position is a vector.
        return void
        
        var positions = []
        // build up an array with vectors of all the possible positions:
        for x = 0 to 7 loop
        	for y = 0 to 8 loop
        		positions[len(positions)] = {x, y}
        	repeat
        repeat
        int count = len(positions)
        int amountToPlace = 4 // if you want to place 4 sprites
        for i = 0 to amountToPlace loop
        	int next = random(count)
        	place(positions[next])
                // replace the used position with the next ones (you could also use a splice function for this, to keep the positions array clean at the end)
        	while next < count - 1 loop
        		positions[next] = positions[next + 1]
        		next += 1
        	repeat
        	count -= 1
        repeat
        

        Hope it helps.

        1 Reply Last reply Reply Quote 2
        • Martin
          Martin Fuze Team last edited by Martin

          i think that can probably be simplified a little? But certainly along the same track... For this I'll assume you want to position 10 'things' in random, non duplicate places. I'm also not at my switch so I'll do 'pretend code and let you fill in the gaps / fix any anomolies'

          array things[]
          for n = 0 to 10 loop
              someVal = { random(maxX), random(maxY) }
              while isDuplicate(someVal) loop
                  someVal = { random(maxX), random(maxY) }
              repeat
          repeat
          
          function isDuplicate(newVal)
              int result = false
              int n = 0
              for n = 0 to len(things) loop
                  if newVal == things[n] then 
                      result = true
                      break
                  endif
              repeat
          return result
          

          At the end of that code you should if all goes well have an array of 10 random but not duplicate positions?

          1 Reply Last reply Reply Quote 1
          • SteveZX81
            SteveZX81 F last edited by

            Thanks. I can't get that code to work without erroring but I suspect the fault is mine, I shall keep trying.

            1 Reply Last reply Reply Quote 0
            • PickleCatStars
              PickleCatStars F last edited by PickleCatStars

              Make a 7x8 array, all filled with 0s, and mark the position as 1 if you use it. You can use that to see if a position is already filled.

              1 Reply Last reply Reply Quote 2
              • T
                tony7059 last edited by

                I've done something like this before... different, but similar. I wanted to randomize the positions of consecutive numbers to shuffle them. I think I randomly selected the number for the 1 spot. The random number was based on the max number of array indices. I moved that value at that location to the 1st spot. Then, I shift things in the choice array then, reduce the range of my random call to match the decrease of available indices. So I never had to check for duplicates, because I knew I had a clean number and position to place it in within the shuffled array. It's some extra work to shift items, but if this is an infrequent bit of processing it's no biggy. I needed to do this at the start/restart of a puzzle game I did in another language, but the same principle exists in Fuze.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post