Get Started

Roll the dice

In the previous exercise, you used rand(), that generates a random float between 0 and 1.

As Hugo explained in the video you can just as well use randint(), also a function of the random package, to generate integers randomly. The following call generates the integer 4, 5, 6 or 7 randomly. 8 is not included.

import numpy as np
np.random.randint(4, 8)

NumPy has already been imported as np and a seed has been set. Can you roll some dice?

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

  • Use randint() with the appropriate arguments to randomly generate the integer 1, 2, 3, 4, 5 or 6. This simulates a dice. Print it out.
  • Repeat the outcome to see if the second throw is different. Again, print out the result.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import numpy and set seed
import numpy as np
np.random.seed(123)

# Use randint() to simulate a dice


# Use randint() again

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
745 reviews

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

This chapter will allow you to apply all the concepts you've learned in this course. You will use hacker statistics to calculate your chances of winning a bet. Use random number generators, loops, and Matplotlib to gain a competitive edge!

Exercise 1: Random NumbersExercise 2: Random floatExercise 3: Roll the dice
Exercise 4: Determine your next moveExercise 5: Random WalkExercise 6: The next stepExercise 7: How low can you go?Exercise 8: Visualize the walkExercise 9: DistributionExercise 10: Simulate multiple walksExercise 11: Visualize all walksExercise 12: Implement clumsinessExercise 13: Plot the distributionExercise 14: Calculate the odds

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free