Skip to main content
Corresponding to Sean Francis N. Ballais' comment, it should be the first item whose cumulative sum is above n. For example, if you have 3, the result should be "apple" (4 is the first result above 3). If we take the last cumulative sum (7), the result would be "lemon".
Source Link
lennon310
  • 3.2k
  • 7
  • 19
  • 35

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 44 represents an apple, 44 to below 66 an orange and 66 to below 77 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Go through the list of cumulative sums from start to finish to find the first item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Go through the list of cumulative sums from start to finish to find the first item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Go through the list of cumulative sums from start to finish to find the first item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

Corresponding to Sean Francis N. Ballais' comment, it should be the first item whose cumulative sum is above n. For example, if you have 3, the result should be "apple" (4 is the first result above 3). If we take the last cumulative sum (7), the result would be "lemon".
Source Link

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. FindGo through the lastlist of cumulative sums from start to finish to find the first item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Find the last item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.


Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Go through the list of cumulative sums from start to finish to find the first item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

copy-edited
Source Link
Deduplicator
  • 9.3k
  • 5
  • 34
  • 53

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.

 

Another, slightly more complicated approach would look like this:

  1. Prepare a list of intervals that cover 0 to sum(weights). Each interval represents one fruit, its length beingCalculate the weightcumulative sums of this fruit, so for your exampleweights:

    intervals = [3[4, 56, 6]7]
    

Where an index of 0-3below 4 represents an appleapple, 4-5 to below 6 an orangeorange and 6 to below 7 a lemonlemon.

  1. Generate a random number nn in the range of 00 to sum(weights)sum(weights).
  2. Find the interval in which n falls and you got yourlast item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more processing powercomplicated code than the first, but less memory and computation and supports floating point-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

The simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.

Another, slightly more complicated approach would look like this:

  1. Prepare a list of intervals that cover 0 to sum(weights). Each interval represents one fruit, its length being the weight of this fruit, so for your example:

    intervals = [3, 5, 6]
    

Where an index of 0-3 represents an apple, 4-5 an orange and 6 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights)
  2. Find the interval in which n falls and you got your fruit.

This approach requires more processing power than the first, but less memory and supports floating point weights.

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so

fruits = [apple, apple, apple, apple, orange, orange, lemon]

Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very memory efficient and requires integer weights.

 

Another, slightly more complicated approach would look like this:

  1. Calculate the cumulative sums of weights:

    intervals = [4, 6, 7]
    

Where an index of below 4 represents an apple, 4 to below 6 an orange and 6 to below 7 a lemon.

  1. Generate a random number n in the range of 0 to sum(weights).
  2. Find the last item whose cumulative sum is above n. The corresponding fruit is your result.

This approach requires more complicated code than the first, but less memory and computation and supports floating-point weights.

For either algorithm, the setup-step can be done once for an arbitrary number of random selections.

Removed superfluous "0" from intervals list
Source Link
Loading
Source Link
Loading