Selecting a random list element in python

I'm trying to create a function that takes two lists and selects an element at random from each of them. Is there any way to do this using the random.seed function?

Answers


You can use random.choice to pick a random element from a sequence (like a list).

If your two lists are list1 and list2, that would be:

a = random.choice(list1)
b = random.choice(list2)

Are you sure you want to use random.seed? This will initialize the random number generator in a consistent way each time, which can be very useful if you want subsequent runs to be identical but in general that is not desired. For example, the following function will always return 8, even though it looks like it should randomly choose a number between 0 and 10.

>>> def not_very_random():
...     random.seed(0)
...     return random.choice(range(10))
... 
>>> not_very_random()
8
>>> not_very_random()
8
>>> not_very_random()
8
>>> not_very_random()
8

Need Your Help

Serialize Array] to Json

scala playframework

I'm having an Array[Array[Byte]] and I would like to serialize it to Json in Playframework:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.