Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

method
python
random
communitycreator

How can we use the random triangular() method in Python

Onyejiaku Theophilus Chidalu

Overview

The random.triangular() method in Python is used to return a random floating number, which is between two specified numbers or boundaries (both inclusive).

Syntax

random.triangular(low,high,mode)

Parameters

  1. low: This is a required parameter. We can specify the lower boundary of the random floating number, using this parameter. The default value for this parameter is 0.
  2. high: This is a required parameter. We can specify the upper boundary of the random floating number, using this parameter. The default value for this parameter is 1.
  3. mode: This is an optional parameter. It represents the midpoint between the two boundary values, high and low.

Example

Now, let us write a code to return a random floating number that is greater than or equal to 10, lesser than or equal to 30, but closer to a midpoint of 20.

Code

import random

print(random.triangular(10,30,20))

Explanation

  • Line 1: We import the random module.
  • Line 3: We print the random floating number that is between 10 and 30 (both inclusive), but closer to a midpoint of 20.

RELATED TAGS

method
python
random
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring