I'd like to specify a specific order when selecting Python characters randomly, is it possible?

Asked 2 weeks ago, Updated 2 weeks ago, 10 views

d = np.random.choice(['A', 'B', 'C', 'D'], 30, p=[0.4, 0.4, 0.1, 0.1])
print(d)

From the above code

Can I set it to turn the same character back to random if it comes out 3 times in a row like AAA?

I'd like to have the character I want in a certain place out of the 30 result values (for example, is it possible to have a C in the fifth column?) )

Thank you for your time even though you are busy.

I'd appreciate it if you could answer me

random python

2022-09-19 23:31

1 Answers

random_flag = True

while random_flag:
    random_flag = False
    d = np.random.choice(['A', 'B', 'C', 'D'], 30, p=[0.4, 0.4, 0.1, 0.1])

    for i in range(d.size - 2):
        if d[i] == d[i+1] and d[i] == d[i+2]:
            random_flag = True
            break


I want to have the character I want to appear in a specific place out of 30 result values, is this possible?

I think you can modify it after making random data.


2022-09-19 23:31

If you have any answers or tips


© 2022 pinfo. All rights reserved.