Python Forum
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?
Reply
#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.
My Github
How to post code using bbtags


Reply
#3
Two questions - are you looking for random integers, or random floats? Can numbers be repeated?
Reply
#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)))
Reply
#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!
Reply
#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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 428 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 448 Nov-19-2023, 10:07 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Checking if a string contains all or any elements of a list k1llcod3 1 1,094 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,693 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to change the datatype of list elements? mHosseinDS86 9 1,955 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,113 May-17-2022, 11:38 AM
Last Post: Larz60+
  Why am I getting list elements < 0 ? Mark17 8 3,117 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020