Ide Proyek Python: Belajar Bahasa Coding dengan Menyenangkan
Aplikasi untuk GUI​​ Jam alarm menjadi salah satu ide proyek Python sederhana yang banyak digandrungi. Singkatnya, jam alarm adalah program yang memberi tahu ...
acak
Salah satu ide proyek Python terbaik yang dapat dicoba oleh pemula adalah Rolling Dice Simulator. Pada dasarnya, Anda perlu membuat kode Anda dapat memilih dan mencetak nomor acak dalam kisaran angka satu hingga enam. Untuk melakukan hal demikian, Anda harus terbiasa dengan tipe data integer, modul <anchor>acak</anchor>, dan fungsi print(). Jika Anda ingin simulator bertanya apakah pemain ingin melempar dadu kembali, Anda juga akan membutuhkan tindakan while loop.
Getting to Know the Python random Module
Getting to Know the Python random Module

🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! [START NOW](https://www.bitdegree.org/missions)

[![BitDegree Learn](https://www.bitdegree.org/learn/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org/learn/)

Categories

- [Web3](https://www.bitdegree.org)
- [![HTML](https://www.bitdegree.org/learn/storage/media/images/96f8c890-c194-4dd3-a14e-912d5b185652.svg) HTML](https://www.bitdegree.org/learn/html)
- [![CSS](https://www.bitdegree.org/learn/storage/media/images/f58e503f-af9e-4ef7-85a5-1c7a32c74f02.svg) CSS](https://www.bitdegree.org/learn/css)
- [![PHP](https://www.bitdegree.org/learn/storage/media/images/93006417-fb6a-4196-8349-5c66420615cc.svg) PHP](https://www.bitdegree.org/learn/php)
- [![JavaScript](https://www.bitdegree.org/learn/storage/media/images/0179bd53-228d-43e8-9aa9-b89b29bd3a65.svg) JavaScript](https://www.bitdegree.org/learn/javascript)
- [![SQL](https://www.bitdegree.org/learn/storage/media/images/9c5aedcf-73e6-4884-8848-7c8fe9e7f396.svg) SQL](https://www.bitdegree.org/learn/sql)
- [![Bootstrap](https://www.bitdegree.org/learn/storage/media/images/6dc1de53-7104-4c21-a180-bf07d3fa938b.svg) Bootstrap](https://www.bitdegree.org/learn/bootstrap)
- [![Solidity](https://www.bitdegree.org/learn/storage/media/images/439fb4c2-1d5e-4d96-a712-bc369bf5864a.svg) Solidity](https://www.bitdegree.org/learn/solidity)
- [![jQuery](https://www.bitdegree.org/learn/storage/media/images/c7f108e1-1c83-4ac0-b64a-103e5e231f9b.svg) jQuery](https://www.bitdegree.org/learn/jquery)
- [![Git](https://www.bitdegree.org/learn/storage/media/images/1da96abf-8c23-4fa3-b8d5-9b99e57c3e2e.svg) Git](https://www.bitdegree.org/learn/git)
- [![Chrome DevTools](https://www.bitdegree.org/learn/storage/media/images/91cfa58f-a3ad-487c-b4cf-9877bdbbdb09.svg) Chrome DevTools](https://www.bitdegree.org/learn/chrome-devtools)
- [![C++](https://www.bitdegree.org/learn/storage/media/images/20b7d886-6cb5-40ee-829e-aed4f3e1eccc.svg) C++](https://www.bitdegree.org/learn/c-plus-plus)
- [![Python](https://www.bitdegree.org/learn/storage/media/images/ddd73681-b989-42ef-95fe-4bd0dd2e7698.svg) Python](https://www.bitdegree.org/learn/python)
- [Other](https://www.bitdegree.org/learn/other)

[![BitDegree Learn](https://www.bitdegree.org/learn/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org/learn/)

- [![HTML](https://www.bitdegree.org/learn/storage/media/images/96f8c890-c194-4dd3-a14e-912d5b185652.svg) HTML](https://www.bitdegree.org/learn/html)
- [![CSS](https://www.bitdegree.org/learn/storage/media/images/f58e503f-af9e-4ef7-85a5-1c7a32c74f02.svg) CSS](https://www.bitdegree.org/learn/css)
- [![PHP](https://www.bitdegree.org/learn/storage/media/images/93006417-fb6a-4196-8349-5c66420615cc.svg) PHP](https://www.bitdegree.org/learn/php)
- [![JavaScript](https://www.bitdegree.org/learn/storage/media/images/0179bd53-228d-43e8-9aa9-b89b29bd3a65.svg) JavaScript](https://www.bitdegree.org/learn/javascript)
- [![SQL](https://www.bitdegree.org/learn/storage/media/images/9c5aedcf-73e6-4884-8848-7c8fe9e7f396.svg) SQL](https://www.bitdegree.org/learn/sql)
- [![Bootstrap](https://www.bitdegree.org/learn/storage/media/images/6dc1de53-7104-4c21-a180-bf07d3fa938b.svg) Bootstrap](https://www.bitdegree.org/learn/bootstrap)
- [![Solidity](https://www.bitdegree.org/learn/storage/media/images/439fb4c2-1d5e-4d96-a712-bc369bf5864a.svg) Solidity](https://www.bitdegree.org/learn/solidity)
- [![jQuery](https://www.bitdegree.org/learn/storage/media/images/c7f108e1-1c83-4ac0-b64a-103e5e231f9b.svg) jQuery](https://www.bitdegree.org/learn/jquery)
- [![Git](https://www.bitdegree.org/learn/storage/media/images/1da96abf-8c23-4fa3-b8d5-9b99e57c3e2e.svg) Git](https://www.bitdegree.org/learn/git)
- [![Chrome DevTools](https://www.bitdegree.org/learn/storage/media/images/91cfa58f-a3ad-487c-b4cf-9877bdbbdb09.svg) Chrome DevTools](https://www.bitdegree.org/learn/chrome-devtools)
- [![C++](https://www.bitdegree.org/learn/storage/media/images/20b7d886-6cb5-40ee-829e-aed4f3e1eccc.svg) C++](https://www.bitdegree.org/learn/c-plus-plus)
- [![Python](https://www.bitdegree.org/learn/storage/media/images/ddd73681-b989-42ef-95fe-4bd0dd2e7698.svg) Python](https://www.bitdegree.org/learn/python)
- [Other](https://www.bitdegree.org/learn/other)
- [Sign up Free](https://www.bitdegree.org/connect)

- [Sign up Free](https://www.bitdegree.org/connect)

Code has been added to clipboard!

[Home](https://www.bitdegree.org/learn/) \| [Python](https://www.bitdegree.org/learn/python) \|Random

# Everything You Need to Know About the Python Random Module

Reading time 4 min

Published Oct 3, 2019

Updated Oct 3, 2019

TL;DR – The Python random module contains a set of functions for generating random numbers. The module allows you to control the types of random numbers that you create.

#### Contents

- [1. Why use the Python random module?](#why-use-the-python-random-module)
- [2. Generating Python random integers](#generating-python-random-integers)
- [3. Generating random floating values](#generating-random-floating-values)
- [4. Random functions for lists and sequences](#random-functions-for-lists-and-sequences)
- [5. Creating arrays of random numbers](#creating-arrays-of-random-numbers)
- [6. Python random: useful tips](#python-random-useful-tips)

## Why use the Python random module?

The random module can perform a few different tasks: you can use it to generate random numbers, randomize lists, or choose elements from a list at random.

This module is perfect for generating passwords or producing test datasets. It can also be integrated into Python `for` or `if` loops to change the outcome of a function at random.

## Generating Python random integers

The most basic and common use of the random module is to generate Python random integers. To accomplish this, you will need to import the random module and then use the `randint()` Python function:

**Example** Copy

```hljs python
import random
random.randint(0,10)
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-random-example-1) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

This will output a random number between `0` and `10`, including end-points.

Alternatively, if you want a step size other than `1`, you can use the `randrange()` function. In this case, the syntax is:

**Example** Copy

```hljs python
random.randrange(start, stop[, step])
```

The `random.randrange()` function uses a `step` value of `1` by default. If you specify a `step`, the range of potential outputs is determined using the Python `range()` function.

## Generating random floating values

If you want to generate a random floating value rather than an integer, use the Python `random.random()` function:

**Example** Copy

```hljs python
import random
random.random()
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-random-example-3) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

This will tell Python to generate a random number between `0` and `1`, excluding `1`.

If you want a random float number between specific start and end values, use the `random.uniform()` function:

**Example** Copy

```hljs python
import numpy

uniform = numpy.random.uniform(0, 100, size = (3, 5))
print(uniform)
```

This will tell Python to generate a random float number between `0` and `100`, excluding `100`.

![DataCamp](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-5f96bfc70113e.o.png)

**Pros**

- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced

**Main Features**

- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable

[GET 25% OFF](https://www.bitdegree.org/learn/goon/datacamp)

![Udacity](https://assets.bitdegree.org/online-learning-platforms/storage/media/udacity-review-logo-small.o.png)

**Pros**

- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features

**Main Features**

- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion

[UP TO 70% OFF](https://www.bitdegree.org/learn/goon/udacity)

![edX](https://assets.bitdegree.org/online-learning-platforms/storage/media/edx-logo-60190648f3a51.o.png?tr=w-200)

**Pros**

- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available

**Main Features**

- University-level courses
- Suitable for enterprises
- Verified certificates of completion

[FREE COURSES](https://www.bitdegree.org/learn/goon/edx)

## Random functions for lists and sequences

If you have a list of numbers, values, or other elements, you can use the Python random module to randomly select one or more elements. To choose a single element at random, use the `random.choice()` function:

**Example** Copy

```hljs python
import random

myList = ["bmw", "volvo", "toyota", "chrysler"]
print(random.choice(myList))
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-random-example-5) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

If you want to pick more than one element from a list or sequence, use the `random.sample()` function:

**Example** Copy

```hljs python
import random

myList = ["bmw", "volvo", "toyota", "chrysler"]
print(random.sample(myList, 3))
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-random-example-6) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

In the case that you have a list or sequence and you want Python to randomize the order of elements in the list for you, use the `random.shuffle()` function:

**Example** Copy

```hljs python
import random

myList = ["bmw", "toyota", "volvo", "chrysler"]
random.shuffle(myList)
print(myList)
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-random-example-7) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

## Creating arrays of random numbers

If you need to create a test dataset, you can accomplish this using the `randn()` Python function from the Numpy library. `randn()` creates arrays filled with random numbers sampled from a normal (Gaussian) distribution between `0` and `1`.

The dimensions of the array created by the `randn()` Python function depend on the number of inputs given. To create a 1-D array (that is, a list), enter only the length of the array desired. For example:

**Example** Copy

```hljs python
import numpy

array = numpy.random.randn(3)
print(array)
```

Similarly, for 2-D and 3-D arrays, enter the length of each dimension of the desired array:

**Example** Copy

```hljs python
import numpy

array = numpy.random.randn(3, 5, 2)
print(array)
```

It is possible to multiply the array generated by `randn()` to get values outside of the default 0 to 1 range. Alternatively, you can use the `uniform()` function to set upper and lower bounds on the random numbers generated:

**Example** Copy

```hljs python
import numpy

array = numpy.random.uniform(0, 100, size = (3, 5))
print(array)
```

## Python random: useful tips

- When using `random.random()` to generate a random float number, you can multiply the result to generate a number outside the 0 to 1 range.
- If you want to be able to generate the same random number in the future, check the internal state of the random number generator using the `random.getstate()`. You can reset the generator to the same state using the `random.setstate()`.

[Previous Topic](https://www.bitdegree.org/learn/python-queue) [Next Topic](https://www.bitdegree.org/learn/python-range)

###### Python

[Append](https://www.bitdegree.org/learn/python-append) [Array](https://www.bitdegree.org/learn/python-array) [Class](https://www.bitdegree.org/learn/python-class) [Command Line Arguments](https://www.bitdegree.org/learn/python-command-line-arguments) [Comment](https://www.bitdegree.org/learn/python-comment) [Enumerate](https://www.bitdegree.org/learn/python-enumerate) [Functions](https://www.bitdegree.org/learn/python-function) [Groupby](https://www.bitdegree.org/learn/pandas-groupby) [If... else](https://www.bitdegree.org/learn/python-if-else) [Map](https://www.bitdegree.org/learn/python-map) [Not Equal](https://www.bitdegree.org/learn/python-not-equal) [Print](https://www.bitdegree.org/learn/python-print) [Queue](https://www.bitdegree.org/learn/python-queue) [Random](https://www.bitdegree.org/learn/python-random) [Range](https://www.bitdegree.org/learn/python-range) [Round](https://www.bitdegree.org/learn/python-round) [Set](https://www.bitdegree.org/learn/python-set) [Sorting Lists](https://www.bitdegree.org/learn/python-sort-list) [Split](https://www.bitdegree.org/learn/python-split) [Time](https://www.bitdegree.org/learn/python-time) [Train\_test\_split](https://www.bitdegree.org/learn/train-test-split) [Variables](https://www.bitdegree.org/learn/python-global-variables) [While Loop](https://www.bitdegree.org/learn/while-loop-python)

##### Best-rated MOOCs to Learn Programming:

![DataCamp Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-5f96bfc70113e.png)

9.8

[Read review](https://www.bitdegree.org/online-learning-platforms/datacamp-review)

![Udacity Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/udacity-review-logo-small.png)

9.6

[Read review](https://www.bitdegree.org/online-learning-platforms/udacity-review)

![edX Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/edx-logo-60190648f3a51.o.png)

9.4

[Read review](https://www.bitdegree.org/online-learning-platforms/edx-review)

#### Related Posts

[**Working With a Python Set: Methods, Operators, and Tips** \
Python sets are useful because they can contain different types of objects. Learn everything you need to know about Python set() in this handy tutorial.\
\
4 min read\
\
7 examples](https://www.bitdegree.org/learn/python-set)

[**Splitting Datasets With the Sklearn train\_test\_split Function** \
This tutorial on train\_test\_split covers the way to divide datasets into two parts: for testing and training with the Sklearn train\_test\_split function.\
\
4 min read](https://www.bitdegree.org/learn/train-test-split)

## Related Code Examples

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-range-example-13)

[Output of Using Python Range to Create a List](https://www.bitdegree.org/learn/best-code-editor/python-range-example-13)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-map-example-3)

[The Use of Python Map With Sets](https://www.bitdegree.org/learn/best-code-editor/python-map-example-3)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-queue-example-3)

[Python Queue Example 3](https://www.bitdegree.org/learn/best-code-editor/python-queue-example-3)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-range-example-9)

[Python Range – Positive to Negative](https://www.bitdegree.org/learn/best-code-editor/python-range-example-9)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-class-example-7)

[Output – Representing Complex Data Points With Python Class](https://www.bitdegree.org/learn/best-code-editor/python-class-example-7)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/pandas-groupby-example-4)

[Getting the Number of Even Numbers](https://www.bitdegree.org/learn/best-code-editor/pandas-groupby-example-4)

##### Learn

- [Programming Languages](https://www.bitdegree.org/learn/)
- [Get Certified](https://www.bitdegree.org/free-certifications-online)
- [Sitemap](https://www.bitdegree.org/learn/sitemap)
- [Code Examples](https://www.bitdegree.org/learn/code-examples)

##### Web Development

- [HTML](https://www.bitdegree.org/learn/html)
- [CSS](https://www.bitdegree.org/learn/css)
- [JavaScript](https://www.bitdegree.org/learn/javascript)

##### Server Side

- [PHP](https://www.bitdegree.org/learn/php)
- [SQL](https://www.bitdegree.org/learn/sql)

##### Tools

- [Code Editor](https://www.bitdegree.org/learn/best-code-editor)
- [Color Picker](https://www.bitdegree.org/learn/color-scale)
- [Devtools](https://www.bitdegree.org/learn/chrome-devtools)
- [Git](https://www.bitdegree.org/learn/git)

##### Courses

- [Video Courses](https://www.bitdegree.org/courses/)
- [Code Theory](https://www.bitdegree.org/learn/)
- [Tutorials](https://www.bitdegree.org/tutorials/)
- [Free Certifications Online](https://www.bitdegree.org/free-certifications-online)
- [Online Learning Platforms](https://www.bitdegree.org/online-learning-platforms)
- [Learning Paths](https://www.bitdegree.org/courses/learning-paths)
- [Apply for a Scholarship](https://www.bitdegree.org/courses/scholarships)

##### Company

- [About BitDegree](https://www.bitdegree.org/about-us)
- [EU Projects](https://www.bitdegree.org/about-us/eu-projects)
- [Hiring](https://www.bitdegree.org/hiring)
- [Blog](https://blog.bitdegree.org/)
- [BitDegree Reviews](https://www.bitdegree.org/bitdegree-reviews)
- [FAQ](https://www.bitdegree.org/faq)

##### Legal

- [Policies & Legal Documents](https://www.bitdegree.org/policies)
- [Privacy Policy](https://www.bitdegree.org/privacy-policy)
- [Cookie Policy](https://www.bitdegree.org/cookie-policy)
- [Refund Policy](https://www.bitdegree.org/refund-policy)
- [Terms of Service](https://www.bitdegree.org/terms-of-service)

##### Instructors & Partners

- [Advertise on BitDegree](https://www.bitdegree.org/advertise)
- [BitDegree Studio](https://www.bitdegree.org/courses/studio)
- [Become Our Partner](/cdn-cgi/l/email-protection#7b0b1a090f151e09083b19120f1f1e1c091e1e5514091c)
- [Hire Talents With Us](https://www.bitdegree.org/courses/partners)
- [Learning platforms' reviews](https://www.bitdegree.org/online-learning-platforms)

##### Follow Us

[X](https://x.com/intent/follow?screen_name=BitDegree)[Discord](https://discord.gg/NXNN5tjA7g)[Telegram](https://t.me/BitDegree)[YouTube](https://www.youtube.com/c/BitDegree_org?sub_confirmation=1)[Facebook](https://www.facebook.com/bitdegree.org)[LinkedIn](https://www.linkedin.com/company/bitdegree)[Medium](https://blog.bitdegree.org)[Instagram](https://www.instagram.com/bitdegree/)[Reddit](https://www.reddit.com/r/bitdegree)[GitHub](https://github.com/bitdegree)

Learn to earn: BitDegree free online courses give you the best online education with a gamified experience. Gain knowledge and get your dream job: learn to earn.

[![BitDegree logo](https://cdn.bitdegree.org/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org)

Copyright © 2024 BitDegree.org \| [\[email protected\]](/cdn-cgi/l/email-protection#0b636e6767644b69627f6f6e6c796e6e2564796c)

[![EXCLUSIVE OFFER: GET 25% OFF](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-white.o.png)](https://www.bitdegree.org/goon/datacamp-exclusive-promo)

[**EXCLUSIVE OFFER: GET 25% OFF** Save Big With DataCamp Promo Code](https://www.bitdegree.org/goon/datacamp-exclusive-promo)

Days

Hours

Minutes

Seconds

[GET 25% OFF](https://www.bitdegree.org/goon/datacamp-exclusive-promo)