0

I'm doing stastitics on cointossing. I need a N number of values to be either 0 or 1 based on random numbers.

My code:

import numpy as np

x = np.random.rand(10)

for i in x:
    if i < 0.5:
        i ==  0
    elif i >= 0.5:
        i == 1

print(x)

The code still returns a vector of 10 random numbers between 0 and 1. How can i change my code so it returns a vector of N number of 0 and 1 based on the random vector?

Vuptiman
  • 1
  • 2
  • 1
    Does this answer your question? [Replacing Numpy elements if condition is met](https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met) – Nico Albers Jan 18 '20 at 14:09
  • In a Python loop, changing the iteration variable (here `i`) does not change values in the source (`x`). That's true whether `x` is a list or array. (Unless `i` is a mutable object). Also with a numpy arrays it's usually more efficient to avoid a loop entirely. – hpaulj Jan 18 '20 at 17:33

0 Answers0