简体   繁体   中英

Python : Randomly choose a boolean from an array and change its value

I was wondering if it was possible to create a code like the one quoted.

I mean if it is possible to create an array with booleans (False), choose only one (randomly) and change its value (to True) and then be able to use it?

The first random choice should only be done once, when the program is launched.

Gentleman = False
Daughter = False
Madam = False

character_list = [Gentleman, Daughter, Madam] # an array with the bools above

for character in characters_list:
    character = random.choice(characters_list) # Randomly pick a character from the list
    character = True # Set the character as True

    if Madam == True:
        ...
        Madam = False
        Daughter = True
        ...

    if Gentleman == True:
        ...
        Gentleman = False
        Madam = True
        ...

    if Daughter == True:
        ...
        Daughter = False
        Gentleman = True
        ...

PS: I know my code is wrong but it's just to try to illustrate what I would like to do

Thank you for your help

Update

So after trying what is written in the comment, I find myself with a problem

Gentleman = False
Daughter = False
Madam = False

character_list = [Gentleman, Daughter, Madam] # an array with the bools above
print(characters_list)
characters_list[random.randint(0, len(characters_list)-1)] = True
print(characters_list)

print("Starting up ...")

if Madam == True:
    print("Hello Madam")
    ...
    Madam = False
    Daughter = True
    ...

Output

[False, False, False]

[False, False, True]

Starting up ...

Since I don't go into the if Madam == True: I guess I don't quite understand how to use it, what should I do to get my code to understand that the Madam has turned True and goes into the if statement

(Btw I am new to Python)

To create an array with booleans (False) and randomly change one of it's value (to True) try:

Code:

import random

my_list = [False]*5
print(my_list)
my_list[random.randint(0,len(my_list)-1)] = True
print(my_list)

Input

[False, False, False, False, False]

Output

[False, True, False, False, False]

Explanation

  1. Use randint() function of the random module to randomly select an a number between 0 and length-1 of your list
  2. Use this number as index to replace a random element in your list with a new boolean value

Update

You code seem to warrant a python dict so try modifying the above logic to:

Code :

import random

my_dict = {'Gentleman': False, 'Daughter': False, 'Madam': False}
print(my_dict)
my_dict[random.choice(list(my_dict.keys()))] = True
print(my_dict)

if my_dict['Madam'] == True:
    print("Hello Madam")
    my_dict['Madam'] = False
    my_dict['Daughter'] = True

print(my_dict)

Input :

{'Gentleman': False, 'Daughter': False, 'Madam': False}

Output :

# initial values
{'Gentleman': False, 'Daughter': False, 'Madam': False}

# after random value is set to True
{'Gentleman': False, 'Daughter': False, 'Madam': True}

#inside the if condition
Hello Madam
{'Gentleman': False, 'Daughter': True, 'Madam': False}

Explanation

  1. Initialize your elements as a dict with values as boolean, rather than list
  2. Use random.choice() function of the random module to randomly select an a key from dict
  3. Use this key to replace a random boolean in your dict with a new boolean value

It's not too complicated. Essentially, what you want to do is pick a random index in the the list, and change that item. For example,

import random

bool_list = [False, False, False]

list_length = len(bool_list)

index = random.randint(list_length)

bool_list[index] = True

or, wrapped into a function:

def set_random_true(list_):
    list_len = len(list_)
    index = random.randint(index)
    list_[index] = True

Then, you can use it like this:

import random

def set_random_true(list_):
    --snip--

my_list = [False, False, False]
set_random_true(my_list)

print(my_list)

This would either print [True, False, False] , [False, True, False] , or [False, False, True] . Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM