Domanda

I'm trying to figure out a random amount calculator but by rarity for example:

choices = [10,100,1000,10000]

10 being the most common, 100 more common, 1000 rare, and 10000 extremely rare

I've tried this

import random
def getAmmounts():
    choices = [10, 100, 1000, 10000]
    values = [random.choice(choices) for i in range(10)]
    return values

Which returns a good amount of values but they're not that random 10000 appears quite often when it should almost hardly ever appear when I called on it the data what I received was:

[1000, 10000, 100, 1000, 10000, 10, 1000, 10000, 100, 100]

Two 10000's are in there and hardly any 10 values when 10s and 100s should be most common then an occasional 1000 in the mix but hardly ever a 10000 value. Is there any way to set up a priority type function that does this? Some good example data of what this should return after all said in done would be:

[10,10,100,10,10,100,1000]

And the occasional 10000 but it should be extremely rare, any idea on how to set this up?

È stato utile?

Soluzione

Your code does not assign any probabilities. You might intend for 10 to be less rare than 10000 but Python isn't going to know that.

You can simulate probability using the random.random to generate a random number between 0 and 1 and returning the appropriate number depending on the number generated.

For example

import random

def make_number():
  val = random.random()
  if val < 0.1: #10%
    return 10000
  elif val < 0.3: # 20%
    return 1000
  elif val < 0.6: # 30%
    return 100
  else: # 40%
    return 10

values = [make_number() for i in range(10)]
print (values)

Altri suggerimenti

The input list gets equal probability for each item. Feed it a list with the proportions you want.

choices = [10, 10, 10, 100]

would make 10 three times as likely as 100.

Why don't you generate a number and then assign based on a range:

x = randint(1,11)
if x == 10:
    #This is the rarest, occurring only 10% of the time
elif x < 10 and x >= 8:
    #9 or 8 aka 20%
elif x < 8 and x >= 5:
    #7, 6, 5 aka 30%

You can tweak it how ever you like or make the range longer to get more specific chances.

You can produce a random number

import random
x = random.random() % this produces a random number between 0 and 1

and you can use the randomly generated number to choose between 10, 100, 1000 and 10000:

if x < 0.5:        # x is between 0 and .50, so 50% chance
    choice = 10
else if x < 0.75:  # x is between .50 and .75, so 75-50 = 25% chance
    choice = 100
else if x < 0.9:   # x is between .75 and .90, so 90-75 = 15% chance
    choice = 1000
else:
    choice = 10000 # x is between .90 and 1, so 100-90 = 10% chance (5 times less likely than the first one)

this outputs 10 on 50% of the runs, 100 on 25% of the runs, 1000 on 15% of the runs and 10000 on 10% of the runs. You can customize the ranges to whatever distribution over [10,100,1000,10000] you'd like!

A quick (though not necessarily the best) solution would be to stick multiple copies of the numbers you wish to appear more often in the list:

import random
def getAmmounts():
    choices = [10] * 80 + [100] * 15 + [1000] * 4 + [10000] * 1
    values = [random.choice(choices) for i in range(10)]
    return values
import random

def getAmounts():
  amount = random.random(1,10)
  if amount == 10:
    return 10000
  elif amount > 8:
    return 1000
  elif amount > 5:
    return 100
  else:
    return 10

Or, if you want to use a list, you could do something like (I do not have IDLE open, and it takes a long time to load, so if I'm off a little, I apologize.)

import random

def getAmounts():
    values = [10, 100, 1000, 10000]
    amount = random.random(1,10)
    if amount == 10:
        return values[3]
    elif amount > 8:
        return values[2]
    elif amount > 5:
    return values [1]
  else:
    return values[0]

Using lists, you could always change the actual values you return, so if instead of 10, 100, 1000, 10000, you want to return 50, 500, 5000, and 50000 you could just reassign the values list, and the code still works, without having to go through and change every line.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top