Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 List of n random elements
#1
Hello, I need to make a list of 10 random numbers. A simple way is the following

from random import random
newlist = []
for i in range(10):
    newlist.append(random())
I was looking to make it more elegant (e.g. without using variable i).
I thought something like

newlist2 = [random()]*10
but this of course makes the same numbers. Is there any other way?
Quote
#2
from random import randint
random_numbers = []

for i in range(9):
    random_numbers.append(randint(0,9))

print(random_numbers)


Another way

from random import randint
rn =[randint(0,9) for i in range(9)]
print(rn)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
Johnny's CookBook - Powered By: Python3.8 | Bootstrap4 | Apache2 | Django
Quote
#3
Two questions - are you looking for random integers, or random floats? Can numbers be repeated?
perfringo likes this post
Quote
#4
(Mar-07-2020, 01:32 AM)medatib531 Wrote: I was looking to make it more elegant (e.g. without using variable i).
If you are looking for a way to do this without declaring an auxiliary variable, you can use map, e.g.

list(map(lambda x: random(), range(10)))
Quote
#5
(Mar-07-2020, 04:38 AM)jefsummers Wrote: Two questions - are you looking for random integers, or random floats? Can numbers be repeated?

So random() is just a placeholder, in my program it is an object that creates different instances. Of course I don't want to copy 1 object 10 times but create 10 different instances of it.

(Mar-07-2020, 08:21 AM)scidam Wrote:
(Mar-07-2020, 01:32 AM)medatib531 Wrote: I was looking to make it more elegant (e.g. without using variable i).
If you are looking for a way to do this without declaring an auxiliary variable, you can use map, e.g.

list(map(lambda x: random(), range(10)))
Yes getting rid of the unused variable is nice, thanks!
Quote
#6
Quote:So random() is just a placeholder, in my program it is an object that creates different instances. Of course I don't want to copy 1 object 10 times but create 10 different instances of it.

That's really a different question. Are you asking how to create a list of 10 instances of a class?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 295 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  TypeError indexing a range of elements directly on the list JFerreira 2 295 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  adding elements to a list that are more than a specific number Olavv 2 259 Mar-19-2020, 06:05 PM
Last Post: Olavv
  How do i multiply elements in a list together Olavv 6 352 Mar-09-2020, 10:06 AM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 328 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  Trying to print corresponding elements in two different list Darthmarvolo 5 338 Mar-03-2020, 04:29 AM
Last Post: perfringo
  multiplying elements in a list Olavv 3 469 Feb-27-2020, 04:55 PM
Last Post: DeaD_EyE
  output a list of random numbers 'x' columns wide adityavpratap 4 273 Jan-13-2020, 05:32 PM
Last Post: perfringo
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 334 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  Finding MINIMUM number in a random list is not working Mona 5 407 Nov-18-2019, 07:27 PM
Last Post: ThomasL

Forum Jump:


Users browsing this thread: 1 Guest(s)