• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to pick random integers without repetitions?

Status
Not open for further replies.

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,197
How to do it?
You make a set of possible numbers and return and remove a random element from that set.

For example you use an array list of all possible numbers that can be randomly rolled, the set of possible numbers. You then pick a random index from this array list which is the random number. You then remove this index from the array list before using the number that was picked. Since this array list is backing a set, the order of elements does not matter, and as such you can move the last index to be where the picked index was rather than having to move all following elements down 1 index. You should also have some sort of fail safe should the array list be empty, possibly printing an error message as this is likely caused by a bug.

If your set of numbers is extremely large then backing every number in an array list might not be possible. In this case you will need to do what could be considered the opposite and track the numbers which get rolled and used in a set, possibly backed by an array list. Every time a number is rolled the set gets checked for that number. If the number is in the set of rolled numbers then another roll is performed which then is also checked in an iterative way. If the number is not in the set then it is used and gets added to the set of rolled numbers. Due to this process being iterative it is recommended that there be some cap on the number of attempted rolls made to resolve a number, after which it should default to some non-random algorithm that is guaranteed a complaint result, such as lowest number that is not in the set. This deviation from randomness should be so unlikely to occur that the output can still be considered random. If the set of rolled numbers size limit is reached then some error message should be displayed as this is likely caused by a bug.
 
Dr Super Good said:
You make a set of possible numbers and return and remove a random element from that set.

For example you use an array list of all possible numbers that can be randomly rolled, the set of possible numbers. You then pick a random index from this array list which is the random number. You then remove this index from the array list before using the number that was picked. Since this array list is backing a set, the order of elements does not matter, and as such you can move the last index to be where the picked index was rather than having to move all following elements down 1 index. You should also have some sort of fail safe should the array list be empty, possibly printing an error message as this is likely caused by a bug.

If your set of numbers is extremely large then backing every number in an array list might not be possible. In this case you will need to do what could be considered the opposite and track the numbers which get rolled and used in a set, possibly backed by an array list. Every time a number is rolled the set gets checked for that number. If the number is in the set of rolled numbers then another roll is performed which then is also checked in an iterative way. If the number is not in the set then it is used and gets added to the set of rolled numbers. Due to this process being iterative it is recommended that there be some cap on the number of attempted rolls made to resolve a number, after which it should default to some non-random algorithm that is guaranteed a complaint result, such as lowest number that is not in the set. This deviation from randomness should be so unlikely to occur that the output can still be considered random. If the set of rolled numbers size limit is reached then some error message should be displayed as this is likely caused by a bug.

are you talking about what is possible in wc3 or you talk about programming languages in general?

I want to pick 12 random numbers between 1 and 19 without repetitions and store them in a variables that is used to spawn a player on one of these random locations.
 
Level 20
Joined
Feb 23, 2014
Messages
1,264
All variables are integers and have the initial value equal 0.

1) Setup:
  • Actions
    • Set integer_list[max_index] = (max_index + 1)
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • max_index Less than or equal to (<your maximum value> - 2) // e.g. if you want the maximum value to be 19, this should read "Less than or equal to 17"
      • Then - Actions
        • Set max_index = (max_index + 1)
        • Trigger - Run (This trigger) (ignoring conditions)
      • Else - Actions
2) Selection:
  • Actions
    • Set index_roll = (Random integer number between 0 and max_index)
    • Set selected_integer = integer_list[index_roll] <- THIS IS YOUR OUTPUT VALUE
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • index_roll Less than max_index
      • Then - Actions
        • Set integer_list[index_roll] = integer_list[max_index]
      • Else - Actions
    • Set max_index = (max_index - 1)
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • (Execution count of (This trigger)) Equal to <how many numbers you want to pick> //e.g. if you want to select 12 numbers, this should read "Equal to 12"
      • Then - Actions
        • Trigger - Turn off (This trigger)
      • Else - Actions
Just remember that the amount of numbers that you want to roll must be lesser or equal to your maximum value.
 
Last edited:
Status
Not open for further replies.
Top