0

I have an array in python:

ARR1 = ['A', 'B', 'C', 'D']

and another array, which uses previous array random value for one of the elements

ARR2 = ['1', '2', '3', random.choice(ARR1)]

when I invoke print(ARR2[]) I get random value on last position and this is fine.

Now I have a for in loop:

for i in range(3):
    print(ARR2[])

and I get the same random value every 3 times, like:

['1', '2', '3', 'B'] ['1', '2', '3', 'B'] ['1', '2', '3', 'B']

What I would like is, 5 random values, like:

['1', '2', '3', 'D'] ['1', '2', '3', 'A'] ['1', '2', '3', 'C']

How I understand it, the array ARR2, would have to be initialized, every time it is called in for loop.

Is it doable in Python?

1 Answer 1

1

You created ARR2 once and it already randomized a value when it was initialised. Simply calling it again won't re-sample a random choice from ARR1

If you want it to generate a random choice each time you should do something like:

import random
ARR1 = ["A", "B", "C", "D"]
for _ in range(3):
   ARR2 = ["1", "2", "3", random.choice(ARR1)]
   print(ARR2)
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Nesting ARR2[] creation inside a loop. Thank you @Tamir.
I upvoted, not sure how I accept it :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.