Italy's daily coronavirus death toll and new cases fall

Python Snippet - Random Numbers

Programming, the final frontier......ok, so maybe programming is not really the "final" frontier, but it sure is close. Final frontiers aside, a couple weeks ago we had our second installment into pygame. It was fun, and we added some cool new features. However, after looking over what I wanted to add for the third installment, I decided to go over something in python itself.
Random numbers are needed a lot of places in games, and our little game is no different. After reviewing the power of python's random generators, and realizing I didn't like the Google results for "python random numbers", I decided to do a small tutorial on them myself.
To start with, we need to go ahead and import the random class:
import random
random.seed()
Importing is simple enough, but what about that second line I stuck in there? Well that seeds that random class with something from the system clock, allowing true random generation. Even though a seed is used, it still should be noted that these are said to be "pseudo" random numbers. However, for most purposes, the random class will work for just about all purposes you would run into normally.
So once you import and seed the generator, you can get down to business. The most basic random generation you can do is with, well, the random() method. It gets the next random number between 0.0 and 1.0.
print(random.random())
//0.0522096426784
However, there are ways to get a number in a range, with either an integer or a float. The methods look something like so:
print(random.randint(1,10))
//5
print(random.uniform(3.2, 6.7))
//3.4089421725853639
I am not exactly sure why exactly the random float method is called "uniform", but it is an interesting name to be sure. Now, the next step is to show off the randrange(), which can be used in two ways:
print(random.randrange(1,10))
//5
print(random.randrange(0, 45, 9))
//27
Now the first example is simple, it just gets a random integer in-between the two given. The second example, however, involves a third argument, which is called "the step". The method makes sure that the number picked is divisible by the step, thus it is a step of "the step". The only other factor with the third argument is that the start and end have to be divisible by the step. It's a neat addition.
Quite possibly the coolest thing I ran across was a method called choice, which actually "chooses" a random element out of a sequence type object. To see its full potential, take a look at this:
print(random.choice("a string"))
//t
print(random.choice(["some", "random", "strings"]))
//random
print(random.choice(["a string", 10, 14.32, [1,5,5]]))
//[1,5,5]
It may not seem like much at first, but what python is doing is taking ANY sequence object (include just a basic string, which is a character sequence), and choosing a random element. So, as you can see, you can give this method just about anything. You could pass in a list of lists, and it will pick a list. Its really fun to think of all the random things you can get from this method. Yeah, I just made a cheesy pun.
Another really neat feature of the random generator is a method called shuffle, and it does exactly what it sounds like. You give it a list, and it shuffles the heck out of it. However, note that if you give it a list with "heck" in it, shuffle will not actually remove it, but it will shuffle the elements.
Shuffle actually does the mix-up inline, so the method returns nothing. It makes things just a little bit easier. The implementation would look like this:
shuffleMe = [0,1,2,3,4,5,6,7,8,9]
random.shuffle(shuffleMe)
print(shuffleMe)
//[2, 4, 6, 3, 7, 8, 0, 5, 9, 1]
One last neat feature is the sample method, which actually takes a set number of random elements out of sequence. Since you can give it any sequence object, you can use a string as well, which in always interesting:
pickChars = "some things to pick"
print(random.sample(pickChars, 6))
//['c', 's', 'e', 'n', 'g', ' ']
pickNums = [0,1,2,3,4,5,6,7,8,9]
print(random.sample(pickNums, 5))
//[6, 4, 5, 9, 8]
So, you can just pick something at random in just about any way you can think of. Python makes it so easy, and if you want to research a little more, the python random class also has special random algorithms. It is something look into.
Well, I hope you enjoyed our short examination into random "stuff" generation in python. Just remember, when you need programming help, all you have to do is Switch On The Code.

Comments