Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting random array from list

I have the following code for a two-player game using nashpy. When I calculate my support enumeration it comes back with a list of arrays. How do I randomly choose an array from this list?

For example, when my list is this:

[(array([1., 0.]), array([0., 1.])), (array([0., 1.]), array([1., 0.])), (array([0.5, 0.5]), array([0.66666667, 0.33333333]))]

…I wish to randomly take one array from this list such as [0.66666667, 0.33333333].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I go by doing this.?

My code:

import numpy as np
import nashpy as nash
import random


A = np.array([[1, 2], [2, 0]])
B = np.array([[0, 1], [0, -1]])
game = nash.Game(A,B)
print(game)
eqs = game.support_enumeration() 
 
a = list(eqs)
b = random.choices(a)

>Solution :

Super close! You can just call random.choice(a) where you have random.choices(a), which should work on any list.

Another way to go about would be to generate a random number between 0 and the length of your list and index your list with that random number.

For clarity:

import random

>>> y  = [(array([1., 0.]), array([0., 1.])), (array([0., 1.]), array([1., 0.])), (array([0.5, 0.5]), array([0.66666667, 0.33333333]))]
>>> random.choice(random.choice(y))
array([0.5, 0.5])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading