Sure, here's a simple implementation of a function that does this. The idea is to calculate inverse scores based on the absolute difference between guessed and actual values. The higher the difference, the lower the score. Then we calculate the stake for each user based on their proportional score.
def distribute_tokens(users_guesses, actual_value, total_tokens):
# Calculate scores as inverse of difference between guess and actual value
scores = [1 / abs(guess - actual_value) for guess in users_guesses]
# Normalize scores so they sum up to 1
total_score = sum(scores)
normalized_scores = [score / total_score for score in scores]
# Distribute tokens based on normalized scores
tokens_distribution = [round(score * total_tokens) for score in normalized_scores]
return tokens_distribution
You can use the function like this:
users_guesses = [50, 55, 60]
actual_value = 52
total_tokens = 1000
tokens_distribution = distribute_tokens(users_guesses, actual_value, total_tokens)
print(tokens_distribution)
This will print the distribution of tokens for each user based on their guesses and the actual value.
Please note that this function uses the inverse of the absolute difference as score. If the guesses can be negative, you might want to use the square of the difference instead, to ensure the score is always positive. Also, the function currently does not handle the case where a guess is exactly equal to the actual value (it would result in division by zero). You might want to add a small constant to the denominator to prevent this, or handle this case separately.