Python Programming Tutorials - Random Number

in python •  7 years ago 

python-logo.png
In order to use random numbers I first will need to look at the random number function.
There is actually more than one but the one that we're going to be looking at is randint, so you put the function name randint I put bracket in and I've got to say a stock number I'm going to have one for now and the end number which is 6. Like this

Now this start and end number base says this is a lowest number one's role and the highest number one’s role.

Now random number on its own is not particularly very useful I probably want to print that out so we put the word print in front of it. Like this

>>> print randint(1,6)

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print randint(1,6)
NameError: name 'randint' is not defined
>>> 

Now this isn't actually going to do anything if I execute it, it tells me randint is not defined that's because it doesn't know about randint. The reason why it doesn't know about it it's because random numbers are actually stored in a module and also to use random numbers I need to import that module.

Python is a very powerful language and most of the extra features or the or the power of Python is held in all these different modules, so to use a module I need to first import it in. So use the word import and then random.

>>> import random

Ok, the module name is random and you can then obviously just import it in. Now when you do import random you should always put your imports at the top of your page and you only need to do it once. So if I want to use random numbers maybe roll to free random numbers like that, I only need to import random once.

Now again this isn't going to work because there's one last thing I need to do is it still doesn't know about randint and that's because I need to essentially tell it which module randint is held in, so actually the full code becomes random dot randint 1 comma 6.

>>> print random.randint(1,6)
2
>>> print random.randint(1,6)
6
>>> print random.randint(1,6)
2
>>> 

So they execute that code now you can see our nails gets three random numbers. To use random numbers you first still need to import the random module, you then need to put the module name random and then put the randint function with the start the lowest value want to roll and the highest value want to roll or surrounded in a bracket sandwich.

Now if I wanted to change the values here I can so I can make it a bigger number like this so this is going to roll a random number between 4 and 19 so if execute that

>>> import random
>>> print random.randint(4,19)
18
>>> print random.randint(1,6)
2
>>> print random.randint(1,6)
4
>>> 

as you can see it gives me the value of 18. For really again because it gives me a different value.

So that's going to choose random numbers 4 between for 19. So if I used to use random is and obviously I can use it to do some a bit more adventurous, for example might want to write a simple game which allows you to roll a dice and if the dice roll is 1 you win of wise you lose.

import random

dice = random.randint(1,6)

if dice == 1:
    print "you win"
else:
    print "you lost with the roll off ", dice

So I can store the random number into a variable like that, I can then make decision on it using an if statement, so if dice equals 1 print you win else prints you lost with the role of comma dice.


Very very simple program if execute it you see you lost with the role of 5.


execute again you lost with the role of 3.


Again roll of 3, let's just see if it gets wins there you go I rolled a 1 there. So there you go that's uhm that's using random numbers, later on we'll look into more detail on the input random because I'm sure there's going to be a few viewers in the in the wild word which is going to say well why can't I just use from we'll talk about from and all the different stuff when we look at the input statement in more detail in a later tutorial.

Just to finish this tutorial off I've just opened up the Python Docs and open up the random page, okay not a random page the random page. So um this is not particularly very user friendly I've shown these pages to students in the past and they've taken one look at it went what the hell is this.

I can understand why you're saying that because it's not designed for the beginner programmer, but I'm just going to get used to it because actually there's a lot of really useful information on the docs and as you get better as a programmer you'll be using it more and more and more, certainly I will have these pages open on a very regular basis when I'm doing some proper coding.

So if you scroll down you can see these they're lots of different function session, dot seed, dot getstate, lots different ones. If we scroll down eventually we'll see the randint and that's one we've used.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @alucard14! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

thx u