Home > Software design >  random list from a function
random list from a function

Time:10-08

I need the randomList function parameters to be:

  • the number of elements it should have
  • a low number
  • high number to create the list with.

Im not sure what I am doing wrong. Any help appreciated.

from random import randrange
import random

def randomList(x,y,z):
    myList = []
    for x in range(x, y, z):
        myList.append(random.randrange(x, y, z))
    print(myList)

randomList(1, 5, 1)

CodePudding user response:

For this question I assumed that x would be the amount of numbers added to myList and that y and z would be the minimum and maximums. Using that information I got this code: it iterates x amount of times and each time appends(adds) an integer from (1, 5).

from random import randrange
import random

def randomList(x,y,z):
    myList = []
    for i in range(x):
        myList.append(random.randrange(y, z))
    print(myList)

randomList(5, 1, 6)

This would give 5 random numbers, that have a minimum value of 1 (y) and a maximum of 5(z)(z is exclusive).

The output would look something like the following:

[1, 5, 2, 5, 2]
  • Related