How to create a random value list with specific length with each value at least once within the range in Python?

user12207831

I want to create a random value list with specific length with each value at least once within a range using Python. Below is my Python code:

import random

Start = 1
Stop = 5
limit = 6

RandomListOfIntegers = [random.randint(Start, Stop) for iter in range(limit)]
print(RandomListOfIntegers)

I got an output list at one run as below:

[1, 4, 2, 4, 1, 5]

In the above output within start and stop range (1-5) 3 is missing. But I want the list with each value at least once within the range. My expected output as below:

[1, 3, 2, 4, 1, 5]

Guide me to get the above output. Thanks in advance.

RMPR

You can generate all the numbers, shuffle what you generated then add random number to reach the limit:

import random

Start = 1
Stop = 5
limit = 6
RandomListOfIntegers = list(range(Start, Stop+1))
random.shuffle(RandomListOfIntegers)
RandomListOfIntegers.extend(random.choices(RandomListOfIntegers, k=limit-Stop))
print(RandomListOfIntegers)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get random values in a list to be unique by at least a given amount by each value in the list in Python?

How to get specific string within a value of String in List in Python

Python: how to create a list of lists with increasing length, made of random numbers?

Python - Create a List Starting at a Given Value and End at Given Length

Return specific value when list is out of range in Python

Specific range within a list (python)

How to constrain a value within a range?

generate random numbers within a range with a percentage of them a specific value

How to get a list of some specific weekdays within a date range in python

Pyspark - how to generate random numbers within a certain range of a column value?

Generating a list of N pseudo-random integers, drawn from a specific range, with the list summing to a specific value

How to create a date range on specific dates for each month in Python?

Python | How to group value to different lists from a main list given a certain range of number for each index

.htaccess redirect specific length random value

How to generate a random number within a range once in C?

Random value insertion in a table for specific range

How can I create three random integers which sum to a specific value? (Python)

How to Compare if a value is within the range or not?

How to determine if a value falls within a specific angle range on a circle?

How to sort dataframes within a list by column value within each dataframe?

How to generate a random double value in a specific range using Kotlin?

searching for the highest and the smallest value in a specific range - Random

How to iterate within a specific range of enum class with Hex value in Python

Drop rows of a dataframe that do not contain a specific value at least once

Python - How to create a list of floats when their sizes randomized within a specific range and their sum is predetermined

All permutations of random value within list of values using python

Check if value falls within a specific range powershell

xpath check if specified value exists at least once in each subelement

Count the range of each value in Python