Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Assign a random number to a variable? [closed]

Tags:

python

random

Before I say anything else, I would like to mention that I am almost completely new to coding, and only have a very rudimentary understanding of python.

Now that that's out of the way, my problem is that I am (attempting) to code a text adventure game, along the lines of D&D. I'm stuck at an early stage- namely, how to assign a random integer between 1 and 18 to a variable.

I've seen some ways of doing this, but the value of the variable changes each time it's called up. This can't happen. The reason for this is because I want the stats (Strength, Wisdom, Intelligence, Dexterity, Charisma, and Constitution) to be a randomly generated but fixed number, that can be called upon and have it the same each time.

I've tried mucking around with things like Str = random.randomint(1,18), using the random module.

The closest I've come is using the lambda function, so that when I call up the variable it generates a random number which is different each time. None of these have rally worked, and I would really like to know what I'm doing wrong.

like image 268
user1987349 Avatar asked Jan 17 '13 14:01

user1987349


People also ask

How do you give a variable a random value?

"Str = random. randomint(1,18) should be Str = random.

How do I lock a random number in Excel?

To prevent an Excel random function from recalculating, use the Paste Special > Values feature. Select all the cells with the random formula, press Ctrl + C to copy them, then right click the selected range and click Paste Special > Values.


2 Answers

I see you are very close with what you have:

Str = random.randomint(1,18) should be Str = random.randint(1,18)

This is the line which assigns the random int to the variable Str, and when you ask for Str you should get the same number each time.

If you keep calling Str = random.randint(1,18) it will change Str each time. so only do it once.

If you know or understand about classes you should use them to contain your characters/items/spells/adventures/monsters etc. which can have properties and attributes and inventories etc etc, much like in the game itself, you group all the things under different types.

Note: You mention you used lambda, that might mean that you have assigned Str to a function which returns a random variable each time, meaning each time you asked for Str you would actually be asking for the function to return a random number.

like image 51
Inbar Rose Avatar answered Oct 31 '22 18:10

Inbar Rose


Welcome to coding.

I've been a web developer for a while and I've also recently started to sink my teeth into better things than php (in my opinion). I would also use:

Str = random.randomint(1,18)

Are you using an IDE? I found they're great when you're first learning a new language due to the code completion. It really helps you get to know your way around the language faster, due to not having to go to various websites to look up syntax. Good luck! Text based adventure games are a great way to start learning!

like image 32
imrobertjames Avatar answered Oct 31 '22 16:10

imrobertjames