Random Numbers in Python
There should be a built in way to create random numbers in python, and there is at least one way by making use of the random standard library. There are some projects where I might want to plug in a random number for an expression, or as an argument to a function. The random standard library has not just one, but a few methods to help make quick work with most typical use case examples for random numbers.
In this post I will be going over a few quick examples of the basic methods to get up and running quickly with random numbers in python. There will be just a few quick basic examples of the random method that will return a value between 0 and 1, and a few other range methods that help save me a little time when making my own solutions.
1 - Some Basic random library examples
1.1 - The random method
So lets start out with a very simple example of the random built in library. If I import random I can then use a bunch of methods off of the random module such as the random method.
1.2 - Using the value in an expression
So now that I have the very basic general idea out of the way when it comes to the random methods lets move on to some more examples in which I am doing something with that value. A major part of programing is working out expressions that will create values. These expressions can be composed of a few variables and some times it might be nice to pass in a random number as part of the expression.
Say for example that I want to get a random position between between max values for x and y. One way is to woek out just some simple expressions to do so where there will be a value between 0 and 1 that can be thought of as a percent value of sorts. In the event that this percent value is zero then the result will be the min value that I want, if it is one then the result will be the max value. In such a case I can plug in a random number for this percent value to get a random point between these min and max values.
This example is actually a little over kill as there are range method in the random library that can be used to get the same result, but you get the basic idea. These random numbers can be used in an expression.
1.2 - using the value as an argument to a function
Maybe it is not always such a good idea to just go ahead an place random values into an expression. Generally I like to make expressions as a return value for function. I can then plug in static values for the function, or I can plug in a random number.
2 - Getting a random range
There are a number of functions for getting a range between a min and max value.
2.1 - random int range
The randint method will give be a random integer value between and including a given min and max value. So for example say I want ramdom integer values that are consistent with the values of a dice, I can pass 1 for the min value, and 6 for the max value.
2.2 - random float range
The uniform method is what I can use to get a random number between a min and max value that is a float rather than int value.
3 - Using the math library with the random library
The random library might rarly be used on its own, there are coming up with some basic examples of what can be done with random numbers, but in order to do anything that is truly interesting I am going to want to make use of some additional libraries, or write a whole lot of my own code.
When it comes to other libraries that come to mind to use with the random library one that comes to mind is the math library. There are a few built in functions for doing some basic math operations such as the pow built in function, but in order to do anything really cool I am going to want at least a few basic math functions to work with. So in this section I will be going over some simple examples that make use of random numbers, and the math library.
3.1 - Having some fun with cos and sin
The Math.cos, and Math.sin functions of the math library are two math functions that I find myself playing around with a lot. these methods are great for getting a point along the circumference of a circle when I know a center point, distance from that center point, and an angle. With that set of known values I can create a function where I pass those values to get a point that lays on a circle. It is not so hard to create such a function, and once I have that function to work with I can have fun using the random library to get random points that lay around the circumference of a circle, or any place relative to a given circle.
3.2 - Random numbers and atan2
There are many must know methods in the math library when it comes to having fun with random numbers and actually using those random numbers to position things. The math.cos and math.sin methods are great for positioning an object in a random location relative to a center point, but what about getting an angle from a point to another point, and also the other way around. Another must know math method I think is the atan2 method which is relevant to this kind of task.
4 - Conclusion