LuckyRaven10
LuckyRaven10

Reputation: 13

How do I get random numbers that fully cover a range?

I'm trying to have random integers between (1,5) but the catch is displaying all of the values from the random integer in a for loop ranging of 10 loops. I only have access to randint() and random() methods in 'random class'.

from random import randint

eventList = []
taskList = []
dayList = []

def getEventList():
    eventList.sort()
    return eventList

def getTaskList():
    return taskList

def getDayList():
    return dayList

def generateData():
    while len(getTaskList()) < 10:
        # Need to implement a way to stretch the random int while having all the integers present
        randomEvent = randint(1, 5)
        randomTask = randint(10, 30)
        randomDay = randint(1, 9)

        eventList.append(randomEvent)
        dayList.append(randomDay)

        if randomTask not in getTaskList():
            taskList.append(randomTask)

Upvotes: 1

Views: 521

Answers (2)

Kenny Ostrom
Kenny Ostrom

Reputation: 5871

Based on clarifications in my other answer, I think you meant to ask "how to I get random numbers that fully cover a range?"

You are using randint, and just calling it extra times hoping to get all the values. But depending on random chance, that can take a while.

It would be better to just take all the values you want, e.g. list(range(1,6))
and then just rearrange that with random.shuffle
https://docs.python.org/3/library/random.html#random.shuffle

import random
values = list(range(1, 6))
random.shuffle(values)
print(values)

Obviously, if this is what you want to do, but your prof says you can't use that function, then you should use that function, get working code, and only THEN write your own replacement for the standard library. (so you only have to debug one or the other)

So write your own version of shuffle. I'll leave the details to you, but it should be pretty easy to use randint to pick one of the available entries, removing it, and so on.

Upvotes: 2

Kenny Ostrom
Kenny Ostrom

Reputation: 5871

In order to sort things, you need "things" to sort. Therefore you must group together all three numbers in a single entity, and then sort those entities. I suggest you use a Task class, and let it handle things like generating a unique task number. Avoid global variables.

Here is a minimal reproduction (based on your code) of just taking your three numbers in a tuple, combining them with the builtin "zip" and sorting that:

from random import randint

def generateData():
    eventList = []
    taskList = []
    dayList = []
    for _ in range(10):
        randomEvent = randint(1, 5)
        randomTask = randint(10, 30)
        randomDay = randint(1, 9)
        eventList.append(randomEvent)
        taskList.append(randomTask)
        dayList.append(randomDay)
    return sorted(zip(eventList, taskList, dayList))

for task in generateData():
    print(task)

Also note that python convention for variable names is a little different, but I left that alone and used your names.

Upvotes: 0

Related Questions