Blender Python Tutorial: How to Generate a Random Number

Darkfall
4 min readOct 5, 2020

In this video, we will be looking at how we can create a random number and then displaying that number in a Panel. We will also take a look at adding text and Icons and only displaying them based on that random number.

There are two types of Numbers we can use, Floats and Integers. These are just fancy names for Decimal or Whole Numbers.

After we import bpy, we then need to import random. This will then enable us to generate a random number.

In this example we will be using a Panel, an Operator and a Property Group, though you can use it for a number of different scenarios. I use a template script which was created in a previous tutorial and if you want to follow along, you can download a blank template here.

Alternatively you can download the finished script here.

If you want to Generate a Random Float (decimal) Number we need to create a Custom Property:

If you want an Integer (whole) Number, be sure to change the Property (as shown below):

Now we have created a either a Float or Integer Property, we want to display that Property on our Main Panel.

In the “draw” section of your Panel we can add the Property like so:

You will notice the “mytool” reference. This reference allows us to use any of the Custom Properties within our Property Group. We have covered this in a previous tutorial. Be sure to watch the video here.

At this point, you will have a simple Panel with a Property called “Random Number”. The Idea is, that when the button is pressed, a Random Number will be Generated and then Displayed. To do this we need to go to the “execute” section of our Operator.

For the Float (decimal number):

For the Integer (whole number):

To generate a random Float or Integer is very similar.

For the Float: x = random.uniform(0.0, 1)

We first create a reference called “x”, though this could be named differently. We then use, “random.uniform” for a Random Float Number. Finally we use an argument “(0.0, 1.0)”, this first decimal number is the starting number. Which is then separated by a comma and then we have the end number.

For the Integer: x = random.choice(range(0, 3))

Again we start with a reference (if you already have x as a reference, be sure to use something else). We then use, “random.choice” for a Random Integer Number. The argument for this is different, “(range(0, 3))”. The first number is the starting point though the last number will not be shown.

So if you want the Random Integer to be between 0 and 3 we need to change it to (range(0, 4)).

Now when we run the script and press the button a Random Number has just been Generated!. Though you can not see anything you will just have to take my word, or you can print the random number to the system console.

To do this you simply need to type:

or if you want to get a little fancy, you could add a custom message:

print(“The Random Number is: “, x)

So, we now know the Random Number is working!. We can delete that line of code (since the system console is just for debugging or errors). Then we can display the Random Number by updating our Custom IntProperty that we created before.

This is really simple and requires only one line of code. Right under the Reference:

This code would be the same no matter if it’s an Integer or a Float. We are simply telling the random_number Property to change its Value to x, which is whatever the Random Number is.

We can take this further (as shown in the video), and add text or icons which are displayed depending on the random number.

I hope you find this tutorial helpful and as always thanks for reading.

Originally published at https://darkfallblender.blogspot.com.

--

--

Darkfall

I create Blender Tutorials and more.. Check out our blog or youtube channel!..