+ 55

Is it possible for python to choose a random number from a list like: a = [0, 30, 60, 90]

9th Apr 2018, 4:41 PM
Rok G
60 Respuestas
+ 239
import random a = [0,30,60,90] print(random.choice(a))
9th Apr 2018, 4:51 PM
Hatsy Rei
Hatsy Rei - avatar
+ 33
There is a way from my logic..... you can create a list. then you need to get a random number between 0 to the size of the list. then print the number which it at random number's index for example list = [1,2,3,4,5] now get a random number like we got i = 3 as random number then print (list[i])
9th Apr 2018, 4:49 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 25
Hatsy Rei i think your method is good than my. 🤣🤣
9th Apr 2018, 4:52 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 25
Why do people make it so complicated? import random a = [1,2,3,4,5] pick_random = random.choice(a) print(pick_random) This method is much easier
12th Apr 2018, 7:20 AM
Christian
+ 21
If you want to manipulate random values: https://code.sololearn.com/c0BljV24viV5/?ref=app
9th Apr 2018, 5:53 PM
voja
voja - avatar
+ 10
Thank you very much
9th Apr 2018, 4:52 PM
Rok G
+ 8
import random a = [0,30,60,90] print(a[random.randint(0,len(a)-1)]) https://code.sololearn.com/cp98qc8a2dy5/?ref=app
11th Apr 2018, 11:15 AM
Calviղ
Calviղ - avatar
+ 6
Yes. It’s very easy. Just import the random module, create your array, and type “random.choice(<name-of-your-array>)”!
13th Apr 2018, 4:49 AM
RuthlessDust
RuthlessDust - avatar
+ 6
Is there an easier way to do in java. Instead of int = Math.random()*3; a[int];
13th Apr 2018, 3:26 PM
Evan Martine
+ 6
Mr.Robot in this case 'pseudorandom' is totally sufficient. He doesn't need a high entropy for this. What your concern is about, is security. In that case, yes it is relevant. But saying this, if you know the state of the universe, you could also calculate every random number. Leading this to an end, the definition of the concept of randomness itself is either wrong, or also correct in this case.
18th Apr 2018, 6:03 PM
Loeschzwerg
+ 5
import random #imports random a = [0,30,60,90] print(random.choice(a))#uses the choice method from random to pick a random element from a list.
12th Apr 2018, 6:47 PM
Paul Grasser
Paul Grasser - avatar
+ 5
Thank you for topic i get knowlegde.
19th Apr 2018, 11:40 AM
Kridtapas Rordtook
Kridtapas Rordtook - avatar
+ 4
so interesting how there are so many different ways to get to the same end.
13th Apr 2018, 3:34 AM
JonCantEven
JonCantEven - avatar
+ 4
ARE YOU GUYS SERIOUS???? YOU POST THE ___SAME___ ANSWER 30 TIMES AGAIN AND AGAIN?!
18th Apr 2018, 7:40 PM
Loeschzwerg
+ 3
Mine is to say yes or no, the answer to your question is YES. How? Every code I could think of has been commented.
18th Apr 2018, 4:29 PM
Alexander Wahome
Alexander Wahome - avatar
+ 2
a = [0 , 30 , 60, 90] from random import randrange random_index = randrange(0,len(a)) print a[random_index]
12th Apr 2018, 5:58 AM
MsJ
MsJ - avatar
+ 2
from random import * a=[0,30,60,90] dv=a[randint(0,3)]
13th Apr 2018, 11:14 AM
Віталій Яцук
Віталій Яцук - avatar
+ 2
You can also use sample to pop the value: -> from random import sample -> l = [ 0, 30, 60, 90] -> print(sample(l), l) 30 [0, 60, 90] for example...
15th Apr 2018, 10:40 PM
Loeschzwerg
+ 2
Hatsy Rei, I will ask friends if we can choose random # in C PYTHON
18th Apr 2018, 5:29 PM
Just -in
+ 2
Import random print(random.choice([0,30,60,90]))
3rd Apr 2020, 1:27 PM
Het Fadia
Het Fadia - avatar