- Joined
- Jun 27, 2010
- Messages
- 2,763
How to do it?
How to do it?
You make a set of possible numbers and return and remove a random element from that set.How to do it?
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.
Both. WC3 scripting is quite flexible, especially if you use the Lua virtual machine.are you talking about what is possible in wc3 or you talk about programming languages in general?