-2

I am using the following code:-

my_list = range(1,99) 
list_Odd_Numbers = list(filter(lambda varX: varX % 2 == 1,my_list)) 
print(list_Odd_Numbers)

I want random and infinite loop

SSP
  • 1,937
  • 2
  • 8
  • 7
  • 1
    Possible duplicate of [What does random.sample() method in python do?](https://stackoverflow.com/questions/22741319/what-does-random-sample-method-in-python-do) – Sayse Jun 11 '19 at 11:12
  • What does this have to do with xcode? what have you tried? – Sayse Jun 11 '19 at 11:13

3 Answers3

0

Just import random and then:

import random

my_list = range(1,99) 
list_Odd_Numbers = list(filter(lambda varX: varX % 2 == 1,my_list))
while True: # Not recommended, but since you asked for infinite loop...
    x = random.choice(list_Odd_Numbers) # Pick randomly one number from the list.
    print(x) # or do whatever you need to with this random pick.
Daedalus
  • 295
  • 2
  • 17
0
from random import randint

random_odd_numbers = (val for val in iter(lambda: randint(1, 99), 0) if val % 2 != 0)

for number in random_odd_numbers:
    print(number)

Prints:

...
99
81
57
85
87
95
49
...and so on.
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Here is simple solution, don't need to filter anything

2*random.randint(0,49)+1

If you need 100 numbers in a list

[2*random.randint(0,49)+1 for _ in range(0,100)]

UPDATE

Actually, there is a function in standard Python which does the job

random.randrange(1, 100, 2)
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64