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

How to select different option regardless of randomized variable

The question might be a little misleading, as there are no exact words to express my query shortly. What I want to do is make a random variable, and according to it pick one name from, let’s say, 5 options. Then, pick another name from the same pool of options, however without being the same as the first name.

For example, if I have 5 students (John, Mary, Doug, Brandon, and Ava) and I want to choose between two of them, but not pick the same student twice.

Here is how I proceeded to choose the first variable using random.randint() function.

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

subj = random.randint(1, 3)
    if subj==1:
        subj1=='John'
    elif subj==2:
        subj1=='Mary'
    elif subj==3:
        subj1=='Doug'

(Note: value names are just an example)

Now for the second value, I have had some ideas, however I don’t know how to complete it.
Any help is appreciated!!

Have a great day.

P.S.: As far as I know there are no other similar questions, or at least I couldn’t find any. If there are, please redirect me there.

>Solution :

How about sampling directly from your list? The code below will select two students from your list at random.

import random

students = ['John', 'Mary', 'Doug', 'Brandon', 'Ava']
selection = random.sample(students, 2)

# Optional: Unpacking list
subj1, subj2 = selection
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