Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of n random elements
#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


Messages In This Thread
List of n random elements - by medatib531 - Mar-07-2020, 01:32 AM
RE: List of n random elements - by menator01 - Mar-07-2020, 03:00 AM
RE: List of n random elements - by jefsummers - Mar-07-2020, 04:38 AM
RE: List of n random elements - by scidam - Mar-07-2020, 08:21 AM
RE: List of n random elements - by medatib531 - Mar-07-2020, 03:28 PM
RE: List of n random elements - by jefsummers - Mar-07-2020, 07:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 607 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 579 Nov-19-2023, 10:07 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 590 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,708 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Checking if a string contains all or any elements of a list k1llcod3 1 1,273 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  List of random numbers astral_travel 17 3,299 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,709 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to change the datatype of list elements? mHosseinDS86 9 2,165 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,458 May-17-2022, 11:38 AM
Last Post: Larz60+
  Why am I getting list elements < 0 ? Mark17 8 3,315 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