- Home /
Is there a better way for randomization for triggering a percentage of the time
Hello everyone so currently I am choosing random game objects to trigger think of them like different mini-games. I want them to each have a certain percentage to activate to keep stuff active such as 25,25,20,20,10 and I have written the code below and it does the job but not very well just want to see what others suggest! even if its something as simple as making random.range more random all answers are helpful!!
void SwapMachine()
{
if (lastMono != null && lastMono.doneTurn())
{
int index = Random.Range(0, 100);
Debug.Log(index);
if (index <= 25)
{
Right = 0;
Up = 0;
Down = 0;
Slot = 0;
TriggerMiniGame(gameEvents[0]);
Left++;
// if(Left > 3)
Reset(Left, 0);
}
else if (index > 25 && index <= 50)
{
Left = 0;
Up = 0;
Down = 0;
Slot = 0;
TriggerMiniGame(gameEvents[1]);
Right++;
// if(Right > 3)
Reset(Right, 1);
}
else if (index > 50 && index <= 70)
{
Left = 0;
Right = 0;
Down = 0;
Slot = 0;
TriggerMiniGame(gameEvents[2]);
Up++;
//if (Up > 3)
Reset(Up, 2);
}
else if (index > 70 && index <= 90)
{
Left = 0;
Right = 0;
Up = 0;
Slot = 0;
TriggerMiniGame(gameEvents[3]);
Down++;
//if (Down > 3)
Reset(Down, 3);
}
else if ( index > 90)
{
Left = 0;
Right = 0;
Up = 0;
Down = 0;
TriggerMiniGame(gameEvents[4]);
Slot++;
// if(Slot > 3)
Reset(Slot, 4);
}
else
{
Debug.Log("should not trigger, something is wrong");
}
}
}
While I see a few things to suggest in an answer, we need a little more information. Having your code is excellent, but what we need to better understand is:
What is meant by "keep stuff active such as 25,25,20,20,10". I don't have sufficient context to understand what this means, or what this sequence of numbers represents.
This phrase, "it does the job but not very well". From what I see in the code, I can infer the job it is doing, but I can't divine what "not very well" actually means. There is some difference between what it is doing and what you prefer, but I need the meaning of that fleshed out in order to select among a dozen ideas I could suggest.
Your "greater than" checks are unnecessary because the "if" statements before them takes care of those indexes. If you make those changes, your code will be easier to read and less likely to have bugs in the future if you ever decide to change the percentages. Other than that, it looks good!
Example:
if (index <= 25)
{
// same code as before
...
}
else if (index <= 50)
{
...
}
etc.
Define "not very well". This code should do exactly what you described.
If you want more randomization you could consider modifying your code to store your last object and exclude it from next draw
Answer by SunnyChow · Aug 23, 2018 at 06:59 AM
When i do weighted randomization, i like to use to group up the weight and feedback as an object, sum up the weight, and do a randomization in a loop. it's easier for me to add item or change weight.
class Item{
public float weight;
public System.Action onTrigger;
}
List<Item> list = new List<Item>();
list.Add(new Item(){weight=5,onTrigger=delegrate{
DoThis();
}});
list.Add(new Item(){weight=5,onTrigger=delegrate{
DoThat();
}});
float weightRnd = 0;
foreach(Item item in list){
weightRnd+=item.weight;
}
weightRnd*=Random.value;
foreach(Item item in list){
if(weightRnd<item.weight){
item.onTrigger();
break;
}else{
weightRnd -=item.weight
}
}
Not gonna lie I don't really understand the code with the weight and everything but I believe this is what I'm looking for or at least in the ballpark!
your "25,25,20,20,10" numbers are the weights. okay! i just found an article about this metohd. (https://medium.com/@peterkellyonline/weighted-random-selection-3ff222917eb6)