-1

My task is to create two functions: function 1: checks if the randomly generated list is all unique integers function 2: generates a random list of integers.

my function 1 works. my question is for function 2: I know how to randomly generate integers... But I really can't figure out how to randomly generate a list?

import random
#check for unique integers in list
def allunique(x):
    unique_or_not= []
    for item in x:
        if item in unique_or_not:
            return False
        unique_or_not.append(item)
    return True


#need 3 inputs of the # of values to generate, starting # in range of list, ending # in range of list
def list_of_nums():
    num_of_values= int(input("Please enter the number of values you wish to generate:"))
    start= int(input("Please enter the starting # of the values you wish to generate:"))
    end= int(input("Please enter the end # of the values you wish to generate:"))



    for i in range(0,num_of_values):
#have to loop to do it a certain amount of times, append value immediately to list:????
kora
  • 19
  • 4
  • 3
    How does this differ in any meaningful way from [your question yesterday](https://stackoverflow.com/questions/74842576/how-can-you-make-a-list-of-completely-random-integers), where you were provided a link showing this has already been answered? – pjs Dec 19 '22 at 22:20

5 Answers5

2

If you need a set of length n of random numbers in range r use:

import random
def random_int_set(n,r):
  list = set()
  if n > r:
      return set()
  while(len(list) < n):
    list.add(random.randint(0,r))
  return list
test = random_int_set(3,5)
print(test)

================================EDIT======================================= If you need a list of length n of random numbers in range r use:

import random
def random_int_list(n,r):
  list = []
  for i in range(0,n):
    list.append(random.randint(0,r))
  return list
test = random_int_list(3,5)
print(test)
Jip Helsen
  • 1,034
  • 4
  • 15
  • 30
0

You already know how to call randint once. Just do it multiple times. This could be put into a list comprehension for easy return.

def list_of_nums():
    num_of_values= int(input("Please enter the number of values you wish to generate:"))
    start= int(input("Please enter the starting # of the values you wish to generate:"))
    end= int(input("Please enter the end # of the values you wish to generate:"))
    return [random.randint(start, end) for _ in range(num_of_values)]

If you want to guarantee unique integers from list_of_nums, you could use a set to test them before return.

def list_of_nums():
    num_of_values= int(input("Please enter the number of values you wish to generate:"))
    start= int(input("Please enter the starting # of the values you wish to generate:"))
    end= int(input("Please enter the end # of the values you wish to generate:"))
    retval = set()
    while True:
        num = random.randint(start, end)
        if num not in retval:
            retval.add(num)
            if len(retval) == num_of_values:
                return list(retval)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

You could use a set and random.randint:

import random

def generate_items(start, end, amount):
  if start < end and not amount < end-start:
    return ("Invalid values entered")

  result = set() # set, to make results unique
  while len(result) < amount:
    result.add(random.randint(start, end))
  return result


print(generate_items(2, 15, 5))

Out:

{3, 6, 9, 13, 14}
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

EDIT:

Use the sample function in the random library.

import random
def list_of_nums()
...
    for i in range(0, num_of_values):
        myrandlist = random.sample(range(start, end), num_of_values)

The first parameter of the sample function is a list of numbers. As we want integers, we can get a list of numbers via the range function. The second parameter calls for an x number of elements that the list should contain.

--

Original Answer:

Try generating random numbers and append them to your list.

In your case, a "randomly generated list" is a list composed of integers produced randomly. This goal can be achieved by using the random import to add random integers to your list.

Your for loop requires adding numbers to this list. This can be achieved by functions random.randrange() or random.randint(). For a direct approach with random integers ranging from 1-5:

def list_of_nums():
    ... #previous code you've mentioned
    randomintlist = []
    for i in range(0, num_of_values): #where your original for loop begins
        randomint = random.randrange(1, 6) #add +1 to the end value to include 5
        randomint = int(randomint) #convert random type to int type
        randomintlist.append(randomint)
    return randomintlist #to give info on the list you've just made

As you've provided a start/stop variable for your function, you can replace numbers 1 and 6 in the example with the corresponding variables:

    randomint = random.randrange(start, end)
poiboi
  • 84
  • 9
0

You can use random.sample(range(start, end), num_of_values) instead of the for loop:

import random

def list_of_nums():
    num_of_values= int(input("Please enter the number of values you wish to generate:"))
    start= int(input("Please enter the starting # of the values you wish to generate:"))
    end= int(input("Please enter the end # of the values you wish to generate:"))
    return random.sample(range(start, end), num_of_values)
Bruno Lima
  • 25
  • 4