Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python random.betavariate() Method



The random.betavariate() method in Python generates a random floating-point number that follows a Beta distribution. The Beta distribution is a family of continuous probability distributions defined on the interval [0,1]. It depends on the values of two positive parameters, alpha () and beta (), both of which must be greater than 0.

This Beta distribution is widely used to model random variables that are constrained to a finite interval, making it an ideal choice for representing percentages and proportions across various fields.

Syntax

Following is the syntax of the betavariate() method −

random.betavariate(alpha, beta)

Parameters

This method accepts the following parameters −

  • alpha: This is the first shape parameter of the Beta distribution.

  • beta: This is the second shape parameter of the Beta distribution.

Return Value

This method returns a random floating-point number that follows a betavariate distribution. This number will always be in the range [0,1], inclusive.

Example 1

Let's see a basic example of using the random.betavariate() method for generating a single random floating point number.

import random

# Alpha and beta for the Beta distribution
alpha = 2
beta = 3

# Generate a random number from the Beta distribution
random_value = random.betavariate(alpha, beta)

print("Random value from Beta distribution:", random_value)

Following is the output −

Random value from Beta distribution: 0.6127913057402181

Note: The Output generated will vary each time you run the program due to its random nature.

Example 2

This example generates a list of random floating-point numbers using the random.betavariate() method.

import random

# Alpha and beta for the Beta distribution
alpha = 2
beta = 5

# Generate a sample of 10 random numbers from the Beta distribution
random.seed(100)
sample = [random.betavariate(alpha, beta) for _ in range(10)]

print("List of random numbers from Beta distribution:", sample)

While executing the above code you will get the similar output like below −

List of random numbers from Beta distribution: [0.08771503465642065, 0.3103605168954117, 0.20939661454390773, 0.2837877667783816, 0.1266513481787254, 0.14773097492841658, 0.2744865269236881, 0.15785506249274603, 0.2810919299409675, 0.5240571971266883]

Example 3

Here is another example that uses the random.betavariate() method to generate a list of 1000 random floating-point numbers from a betavariate distribution. It then calculates and prints the average of these numbers.

import random

# Define alpha and beta for the Beta distribution
alpha = 3
beta = 1

# Generate a sample of 1000 random numbers from the Beta distribution
random.seed(100)
sample = [random.betavariate(alpha, beta) for _ in range(1000)]

# Display the average
print("Average of the sample: ",round(sum(sample) / len(sample), 2))

The output of the above code is as follows −

Average of the sample:  0.75
python_modules.htm
Advertisements