0

I'm trying to write a python program that will print me a random numbers array 1 thur 100, and also print out the max value of the array. This is what I have so far:

import random
import timeit
print [random.randint(0,100) for r in xrange(10)]
print "Max number in array is",
3
  • 3
    Have you looked at, let's say, the max function? Commented Jul 21, 2013 at 21:09
  • sorry I'm a novice with Python, I thought I was using the max function correctly, I was wrong. Commented Jul 21, 2013 at 21:14
  • @ChrisSanders If you don't use it at all, you definitely don't use it correctly. Commented Jul 21, 2013 at 21:31

3 Answers 3

1

Save the random numbers in a variable, data. Then take the max with max(data).

data = [random.randint(0,100) for r in xrange(10)]
print(data)
print("Max number in array is {}".format(max(data)))
Sign up to request clarification or add additional context in comments.

Comments

1

You'll need to store the array as generated in a variable, and just use max on it to get the maximum value;

import random
import timeit
my_array = [random.randint(0,100) for r in xrange(10)]
print my_array
print "Max number in array is", max(my_array)

Comments

1
import random
rand_ar = [random.randint(1,100) for r in xrange(10)] # randint(0,100) includes 0 also.
print rand_ar
print "Max number in array is %i" % max(rand_ar)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.