Kevin Bryant
Kevin Bryant

Reputation: 11

Randomize value of given key of Dictionary in Python

So I am practicing python and I'm using a tutorial where you create a terminal based game using python. I'm trying to not follow it letter for letter so I can learn it better. In it I have 2 dictionaries, one for the 'Monster' and 'Player'

 player = {'name': 'Kevin', 'attack': 10, 'heal': 16, 'health': 100}
 monster = {'name': 'Grog', 'attack': 12, 'health': 100}

I want to randomize the 'attack' damage between a set number, how would I implement that?

Upvotes: 0

Views: 78

Answers (3)

Mat-KH
Mat-KH

Reputation: 76

If you ever want to pick a number that doesn't come from a consecutive range, use random.choice:

import random
random.choice(range(0,101,5))

Upvotes: 0

Basu_C
Basu_C

Reputation: 410

You can use random to generate random numbers or choices.

import random
r_num = random.randint(a,b)

Return a random integer N such that a <= N <= b.

Upvotes: 1

Thomas Schillaci
Thomas Schillaci

Reputation: 2453

You can do this, change your dictionary to:

player = {'name': 'Kevin', 'attack': [8, 12], 'heal': 16, 'health': 100}

And the code to chose your attack damage:

import numpy as np
np.random.uniform(*player['attack'])

10.240264593076365

Please note that 12 will be excluded from the range.

Upvotes: 1

Related Questions