简体   繁体   中英

Find smallest value in list

Very simple program here. I'm trying to take a positive integer n, generate a list of n random numbers between 100 and 500, and return the minimum value that appears in the list

import random
def randomNumber2(n):
    mylist2 = []
    needMoreNumbers2 = True
    while (needMoreNumbers2):
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
        n >= n - 100
        if (n <= 500):
            needMoreNumbers2 = false
    return myList2

Could someone help guide me in the direction I need to go?

import random

def randomNumber2(n):
    return min(random.randint(100, 500) for _ in range(n))

If you want something closer to your program, you need to fix a number of things in your program first - it doesn't run at all. Python is case-sensitive, so mylist2 and myList2 are different. Use the same spelling throughout. Similarly, false is not a Python builtin - False is.

Fix those and we get:

import random

def randomNumber2(n):
    myList2 = []
    needMoreNumbers2 = True
    while (needMoreNumbers2):
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
        n >= n - 100
        if (n <= 500):
            needMoreNumbers2 = False
    return myList2

Now regardless of what's passed for n , so long as n is <= 500 that returns a list with a single random integer, in 0 thru 499 (inclusive). If n is > 500, it runs forever (infinite loop). Can you see why? if (n <= 500) is always true if the original n is <= 500, and always false otherwise. Your n >= n - 100 doesn't do anything visible (it just compares n to n - 100 , and always returns true, which is ignored).

So take the advice to move toward more reasonable Python idioms. Here's a way that should be easier to understand:

import random

def randomNumber2(n):
    myList2 = []
    while len(myList2) < n:
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
    return myList2

Progress ;-) Now we at least get back a list with n random integers. They're not in the range you want, though: int(random.random() * 500) returns an int in the range 0 through 499 inclusive. If you want them to be between 100 and 500 inclusive, the best way is to do random.randint(100, 500) , as someone else already showed you. 100 + int(random.random() * 400) is an equivalent way that's closer to your original code, so let's try that:

import random

def randomNumber2(n):
    myList2 = []
    while len(myList2) < n:
        randomNumber2 = 100 + int(random.random() * 400)
        myList2.append(randomNumber2)
    return myList2

To get the minimum, it's overwhelmingly best to just do min(myList2) at the end. Not only will that be correct, it's also faster than anything you can code yourself. If you want to do it yourself, here's one way:

import random

def randomNumber2(n):
    myList2 = []
    smallest = None
    while len(myList2) < n:
        randomNumber2 = 100 + int(random.random() * 400)
        myList2.append(randomNumber2)
        if smallest is None or randomNumber2 < smallest:
            smallest = randomNumber2
    assert smallest == min(myList2)
    return myList2

The answer from FJ is how most experienced Pythonistas would code it, though.

I'm trying to take a positive integer n, generate a list of n random numbers between 100 and 500, and return the minimum value that appears in the list

Then, you don't need the list, you just need to return the smallest of the random numbers generated in n "throws"

import random

def smallrand(n,rangemin=100,rangemax=500):
    smallest = float('+inf')
    for _ in xrange(n):
       new = random.randint(rangemin, rangemax)
       if new < smallest: smallest = new
    return smallest

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