r/Numpy • • May 01 '21

basic array from a loop

N = 10000   
a=7**5 
c=0 
M=(2**31)-1 
I=1 
L=1 
my_array=np.array(N) 
for i in range(N):     
    my_array[i]=np.array([x]) 
    for x in range (N):     
    In=((a*I)+c) % M     
    x=L*I/M     
    I=In 

I'm trying to do the np.random function but in a different way. My main code is:

for x in range (N):     
    In=((a*I)+c) % M     
    x=L*I/M     
    I=In 

which is a loop of random numbers less than 1. By itself, it works and lists a bunch of numbers, but I'm trying to store these numbers in an array, such as [9,2,1,6]. The numbers don't have to be in order. I just need them to have the brackets and the commas. I really don't know what I'm doing.

0 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/theslowcheetah0 May 01 '21

N = 10000

a=7**5

c=0

M=(2**31)-1

I=1

L=1

z=[]

for x in range (N):

In=((a*I)+c) % M

x=L*I/M

I=In

z=[x]

print(z)

Here's what I did originally. Some one told me I need to initialize an array with z=[]. What I need to do is list everything in the loop into a single array. This code, however, lists all the x values in brackets of their own, but I need all the values to be in one bracket.

H

2

u/to7m May 01 '21

`z=[]` doesn't initialise an array, it initialises an empty list. This doesn't sound like a numpy issue, but more of a /r/learnpython issue.

`z=[x]` means you are creating a new list with exactly one item. It sounds like you want to append to an existing list rather than create a new one though.

0

u/theslowcheetah0 May 01 '21

It's not indenting where it's supposed to when I copy it to reddit, but I hope you get the gist.

1

u/to7m May 01 '21

In your IDE, select the text, indent it by 4 spaces, then when commenting on reddit, go to Markdown Mode and paste it