Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

(This is based on my answer for a similar question on Stack Overflowa similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying the probability of each event. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying the probability of each event. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying the probability of each event. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

added 22 characters in body
Source Link
Michael Madsen
  • 1.8k
  • 17
  • 14

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying weightsthe probability of each event. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying weights. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying the probability of each event. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).

Source Link
Michael Madsen
  • 1.8k
  • 17
  • 14

(This is based on my answer for a similar question on Stack Overflow.)

It sounds like you're asking for a more flexible way of specifying weights. For that, you can use a simple weighing algorithm: simply decide how common each event should be and assign it a weight that is appropriate compared to the other weights. For example, if you have events A, B and C, with probabilities 70%, 25% and 5%, you could give them the weights 70, 25 and 5 (or 14, 5, and 1 - the important thing is the relative difference).

Once you have that, you can use the following algorithm to select an event:

Given a list L of items (I,W), where I is the item and W is the weight:

  1. Add all of the weights together. Call this sum S.
  2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
  3. Initialize a variable to 0 to keep track of the running total. We'll call this T.
  4. For each item (I,W) in L:
    1. T=T+W
    2. If T > R, return I.

It's up to you if you want to first select between the different groups of events, or if you want a single table with all of the events (where each "group" has an appropriate sum compared to the others).