0

This is a design pattern I see coming up a lot in game design and game programming. I've written the code in Java, but this is more about algorithm design.

I want my program to go down various path based on a random chance, and I want to be able to specify the chance for each path.

For example, I want the program to randomly choose whether or not someone gets their last name from their father or mother, or neither, with different weightings for each of those paths.

    //Last Name
    int lastNameProc = rand.nextInt(100);

    if(lastNameProc < 6){ //Orphan or Disowned by father
        if(lastNameProc < 2) { //Orphan
            lName = "Smith";
        } else { //Disowned
            lName = mother.lastName;
        }
    } else { //Not Orphan or Disowned
        lName = father.lastName;
    }

This can also be done with inequalities instead of nested if statements. The problem with doing it that way is that it's not obvious from the else-if clauses what percentage chance an event has of occurring.

Is there a common or better way of doing this?

Another way to phrase this question: If I had a sword in an RPG where on a hit the sword applies one of 4 effects, fire, cold, radiant or necrotic damage, with the probability of each being 0.1, 0.2, 0.2 and 0.5. How would I program this without using nested if statements or inequality based if-else statements. I believe this is called an on-hit proc in game design terms.

My question is quite similar to "Generate A Weighted Random Number" however I am unable to translate the code in the answer because I can't figure out which types to use. It would be nice to see a strongly typed example.

Finally, I'm not sure if these are the correct names for this pattern and I'll edit the title to include the correct words.

Estiaan J
  • 31
  • 1
  • 7
  • 1
    This *might* be a dupe of https://stackoverflow.com/questions/8435183/generate-a-weighted-random-number if I'm reading the question correctly – Martheen Oct 08 '20 at 09:09
  • It's quite similar, but I don't think the selected answer to that question would work here – Estiaan J Oct 08 '20 at 09:13
  • 1
    Why won't it work? It seems your problem is just generating layers of weighted random number, so for each decision branch you generate another number – Martheen Oct 08 '20 at 09:16
  • 2
    Does this answer your question? [Generate A Weighted Random Number](https://stackoverflow.com/questions/8435183/generate-a-weighted-random-number) – Slava Medvediev Oct 08 '20 at 09:28
  • I actually think both of the above might be the answer I'm looking for, but I don't really understand the code involved enough to say yes for sure. – Estiaan J Oct 08 '20 at 09:47
  • If I understand this correctly, you have several branching level. On each level, you need to randomly pick an option with a predefined weight (or perhaps calculated from previous decision). If so, by generating a weighted random number, you can wrap it into simple function where you send a Map with the option as key and the respective weight as the value, and get a random option returned. You'll then use this function on each level. Is that correct? – Martheen Oct 08 '20 at 10:05
  • I don't really know what you mean @Martheen I made an edit that re-words the question. – Estiaan J Oct 08 '20 at 10:20
  • Yeah, this is exactly a dupe – Martheen Oct 08 '20 at 10:22
  • I understand that it's similar, however the code in the "dupe" question is not java. I think an answer to this question would be valuable to people. If you can answer it, then please do :) – Estiaan J Oct 08 '20 at 10:25
  • One of the answer is in Java, and even the second part of the accepted answer can be adapted to Java by just modifying slight syntax change – Martheen Oct 08 '20 at 12:46
  • Unless you made an attempt to port it to Java, and show us where you are stuck (if any, it's actually very straightforward), this is a dupe, and otherwise we'd have one question of randomized weight for each language, even if they're just adjusting syntaxes – Martheen Oct 08 '20 at 12:48

0 Answers0