Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Total beginner question
#1
Hi,

first of all, I hope I'm in the right section of the forum.


I decided to learn a programming language and some Youtube videos later, I decided to go for Python.
As a child I used to play around with basic on the C64 and I had some contact to free pascal lately.

So, I think the best way to start for me is to play around with Python. Write little programs for fun like I did back when I was a child.


So, here is my problem. I want to generate random numbers, to simulate a dice roll, for example.
I the Python 3.6.0 documentation I found:

9.6. random - Generate pseudo-random numbers

I read through it and tried out various things but I always get error messages.
My goal was to have a variable get a random value.

So, how is that done? Can someone help me with that?

Thank you! :-)
Sven
Reply
#2
from random import randint

result = randint(1, 6)
This will return a random integer between 1 and 6
Ref: https://docs.python.org/3/library/random...om.randint
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thank you for your answer, Wavic! :-)

Do I understand it correcly, that I have to use the line:

from random import randint (a,b)
only once at the start of the program and can use it multiple times then?

And if I like to use other option, I just add them like:

from random import random
 

?
Reply
#4
random is the module which you import or import a class, method from it.
Yes you can do for another method the same or for few of them separated with a comma.
That way you import only what you need instead of the whole module

You can use it like this too
import random

#      module   method
#          |      |
#          |      | 
result = random.randint(1, 6)
You import here the whole random module and can use all of its classes and methods: module.method(), module.class.method()

https://python-forum.io/Thread-Basic-Modules-part-1
https://python-forum.io/Thread-Basic-Modules-part-2
https://python-forum.io/Thread-Basic-Modules-part-3
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thank you, especially for the extra explanation. :-)
Reply
#6
You may also like to look at what others have done, see: https://pypi.python.org/pypi?%3Aaction=s...mit=search
Reply
#7
Heres something I've come up with when it comes to dice rolls... Hope it helps.

from random import randint

class Die:

    def __init__(self, sides, roll=0):
        self.sides = sides
        self.roll = roll

    def roll_die(self):
        if self.sides==6:
            while self.roll < 6:
                print("You roll a six sided die: " + str(randint(1, 6)))
                self.roll += 1
        elif self.sides==10:
            while self.roll < 6:
                print("You roll a ten sided die: " + str(randint(1, 10)))
                self.roll += 1
        elif self.sides==20:
            while self.roll < 6:
                print("You roll a twenty sided die: " + str(randint(1, 20)))
                self.roll += 1
        else:
            print("Mistakes have been made!")


six_sided_die = Die(6)
six_sided_die.roll_die()
print('')
ten_sided_die = Die(10)
ten_sided_die.roll_die()
print('')
twty_sided_die = Die(20)
twty_sided_die.roll_die()
Reply
#8
I don't think that's what he's really looking for. Your Die object rolls six times at once, and then won't roll again. Something simpler and more to the point would be:
import random

class Die(object):

    def __init__(self, sides = 6):
        self.sides = sides
        self.roll()

    def roll(self):
        self.up_face = random.randint(1, 6)
        return self.up_face
Output:
>>> d6 = Die() >>> d6.roll() 6 >>> d6.roll() 6 >>> d6.roll() 1
Or even simpler:
import random

def roll_die(sides = 6):
    return random.randint(1, sides)
Output:
>>> roll_die() 2 >>> roll_die() 6 >>> roll_die() 4
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Oh i see now. Because I assign 0 to roll then use a while loop saying while roll < 6... He is looking for a re-roll object. I did this example just to show the random.randint() method being repeated for practice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,381 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,137 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,704 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,865 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 3,174 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,708 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 2,653 Aug-27-2019, 02:38 PM
Last Post: Bramen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020