简体   繁体   中英

random list from a function

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)

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM