dimanche 1 avril 2018

Stored value of random.choice getting replaced on calling it twice

I am using Python 3.5. I have three 1D arrays from each of which I want to choose a value randomly. I first choose the random values using random.choice() and store them in a variable. But, the value stored in the variable gets replaced when random.choice() is called a second time. Here is a snippet of my code:

import numpy as np
import random

a_onc = np.array([1,2,3,4,5])
mass_onc = np.array([10,11,12,13,14,15])
age_onc = np.array([23,24,25,21,22,27])

achoice = np.zeros(2)
masschoice = achoice
agechoice = achoice

for u in range (0, 2):

    achoice[u] = np.random.choice(a_onc)
    print(achoice[u])

    masschoice[u] = np.random.choice(mass_onc)
    print(achoice[u], masschoice[u])

    agechoice[u] = np.random.choice(age_onc)
    print(achoice[u], masschoice[u], agechoice[u])

    print("")

Here is the output:

1.0
12.0 12.0
21.0 21.0 21.0

5.0
11.0 11.0
23.0 23.0 23.0

As you notice, the random values chosen from 'a_onc' array were 1 and 5 (in the two runs of the loop). They were stored in 'achoice' array. But, when random values from mass_onc were chosen, the values in 'achoice' also get changed.

Can someone let me know how to tackle this issue?




Aucun commentaire:

Enregistrer un commentaire