Home > Blockchain >  Python:: assign random values but should include specific number
Python:: assign random values but should include specific number

Time:10-16

I know how to create an array includes random values, but I would like to have at least one number equal to specific value, and all random values should be greater than 1 ?

import numpy as np
x=np.random.rand(1,4)
specif_value=3
print(x)
#x=[2 3 1 1]

CodePudding user response:

Read the docs for numpy.random.rand here. It appears you want an array of random non-zero integers, in which case you should be using randint(docs here)

This code will give you an output array with a specific value in the specified index

import numpy as np 

# Creating random array
size = 4
minimum_value = 1
maximum_value = 100
x = np.random.randint(minimum_value, maximum_value, size)

# Including specified value
specified_value = 3
specified_value_index = 2
x[specified_value_index] = specified_value

print(x)

Note that randint requires a maximum value if you want non-zero integers; otherwise it will return values between 0 and the given minimum

  • Related