How to dynamically create a variable
📁 Advanced Python Topics, example, AI
How to dynamically create a variable whose name can contain the value of another variable?
For example, I need to create a variable like this (pseudocode):
Name_%user-number% = valueHow can this be done?
Assume the condition:
Player 1 - win probability 10
Player 2 - win probability 20
In the simple case, we need to add all win probabilities together (i.e., 10 + 20 in the example) and then generate a random number in the range from 0 to the sum of probabilities (up to 30). Then, from the resulting random number, subtract the win probabilities one by one (for example, going from top to bottom), and as soon as the difference becomes less than 0 - you have found the desired Player (winner).
So, an example:
10 + 20 = 30 // sum of probabilitiesRandom(30) = 26.67 // random number26.67 - Log in to leave an answer
Blogs