+ 55
Is it possible for python to choose a random number from a list like: a = [0, 30, 60, 90]
60 Réponses
+ 239
import random
a = [0,30,60,90]
print(random.choice(a))
+ 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])
+ 25
Hatsy Rei
i think your method is good than my.
🤣🤣
+ 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
+ 21
If you want to manipulate random values:
https://code.sololearn.com/c0BljV24viV5/?ref=app
+ 10
Thank you very much
+ 8
import random
a = [0,30,60,90]
print(a[random.randint(0,len(a)-1)])
https://code.sololearn.com/cp98qc8a2dy5/?ref=app
+ 6
Yes. It’s very easy. Just import the random module, create your array, and type “random.choice(<name-of-your-array>)”!
+ 6
Is there an easier way to do in java. Instead of int = Math.random()*3;
a[int];
+ 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.
+ 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.
+ 5
Thank you for topic i get knowlegde.
+ 4
so interesting how there are so many different ways to get to the same end.
+ 4
ARE YOU GUYS SERIOUS???? YOU POST THE ___SAME___ ANSWER 30 TIMES AGAIN AND AGAIN?!
+ 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.
+ 2
a = [0 , 30 , 60, 90] from random import randrange
random_index = randrange(0,len(a)) print a[random_index]
+ 2
from random import *
a=[0,30,60,90]
dv=a[randint(0,3)]
+ 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...
+ 2
Hatsy Rei, I will ask friends if we
can choose random # in C PYTHON
+ 2
Import random
print(random.choice([0,30,60,90]))