Probability in Godot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sumguy05

I’ve googled quite a bit and can’t wrap my head around it. My goal is to have the probability of an item generated that is dependent on player score. How would I even begin this? Does anyone have experience with this in their game?

Edit: Apologies for the delay and being vague. I had a family emergency I tried to code through but just couldn’t. I would like a max probability. I’ll see if i can use LuminousNutria’s code with what i already have and see if it works. I am currently spawning an object that is randomly picking an object amongst an array. I want the probability to be between an array of items. The probability is specific to whether one item spawns or another. So the higher score would be more likely to spawn a negative item. That said now it is equal between a good or a bad item meaning the baseline is a coin flip. I’d like to keep that the odds at a baseline that is even between the array of items but as the player gains points that changes.

Edit: 2

I’ve gotten further along. The following below works for a coin flip situation

randomize()
var rand_index = randi() % options.size()
if randi()%101+1 <= prob:
	return options[1]
else:
	return options[rand_index]
pass

In this example a case statement in which prob is set to relative to the player’s score will be simple enough in selecting between 2 items so far only good and bad. However using that logic for a player chest for instance in which a wide array of items can be selected. I’m not sure how to implement.

How about some simple if else conditions?

codevanya | 2019-01-17 18:39

add Timer and put random generator of 1 or 0. If it give 1 then emit signal to spawn mob if 0 then no.

Zippelin | 2019-01-17 19:14

Hi, Could you give us more details? Do you want a max probability for the item? or can it reach 100%? Usually, i would calculate probability with a function depending of score with asymptote to the max probability (for example max_prob-exp(-score) will be near 0 for low scores, and near max_prob when score is high). Then, i would use a random number between 0 and 1, and if that number is less or equal than the number generated by the probability function, you generate the item. If not, you dont. Something like this?

p7f | 2019-01-18 11:47

:bust_in_silhouette: Reply From: LuminousNutria

As has been mentioned, there are a few different ways to do this. Here’s what I would do.

# if the player's score is high enough to generate the item at all
if player_score > No_Chance:
   # generate a random float between maximum possible chance and
   # the lowest score with any chance
   var chance =  randf()*(Max_Chance_Score+1) + (No_Chance + 1)

   # generate the item if the chance variable is
   # lower than the player score
   if chance <= player_score:
      return true # generate the item
   else:
      return false # don't generate the item

Assuming all the variables and constants here but chance are ints, this code makes it so a player with a score greater than or equal to the Max_Chance_Score has a 100% possibility of generating the item, and a player that has a score equal to No_Chance + 1 has only a 1% chance of seeing the item. Anyone lower has “No_Chance”.

Also, remember to call Randomize().