0

I am attempting to make a randomly generated plot using the LCG method. While the plot behavior works, my issue is when I attempt to return a list to a variable. My goal is to have the recursive function return a list to a variable, but it seems that it does nothing when it exits the definition.

Recursive Function (inside definition):

def LCG_recursive(vmin, vmax):
    for x in range(0, 1):
        randnum = 19680801      # Fixing random state for reproducibility
        randnum2 = []           # clearing list for next random set
        break
    for x in range(1, i+1):
        randnum = ((a*randnum+b) % M) / M
        randres = (vmax - vmin)*randnum + vmin    #limit to range of plot
        randnum2.append(randres)
        if x>i:
            return randnum2

Called From:

# For each set of style and range settings, plot i random points in the box
# defined [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
    
    xs = LCG_recursive(23, 32)
    ys = LCG_recursive(0, 100)
    zs = LCG_recursive(zlow, zhigh)
    ax.scatter(xs, ys, zs, marker=m)

My attempt was to set the seed as randnum and clear randnum2 for every retrieval of a randomly generated list of values. However, attempting to step through the file shows that it performs the recursion successfully, but returns a NoneType object when it moves to the next axis list.

I attempted to create global variables and check to see if I could move randnum2 after I performed the recursive operation, but I haven't gotten any luck.

Apple Cola
  • 27
  • 2
  • 9
  • What is your base case for recursion? – quamrana Sep 22 '20 at 18:31
  • I should clarify that i'm using the range as a sort of counter. I have "i" set to 10 in this case and the recursion unconditionally enters the first range and cycles through the second range until I generate 10 entries – Apple Cola Sep 22 '20 at 19:57
  • So, how is this recursive? – quamrana Sep 22 '20 at 20:10
  • It relies on the previous version of the answer to generate a new answer. What I previously mentioned was just to show at what point I broke out of the loop. I believe that Python has a limit of 1000 for a length of a list so I wanted to make it clear that I was not near that limit. – Apple Cola Sep 22 '20 at 21:46
  • 1
    Ok, you mean iterative, not recursive, And python does not have such a small limit on the length of lists. – quamrana Sep 23 '20 at 07:14

2 Answers2

1

Did you mean:

def LCG_iterative(vmin, vmax):
    
    randnum = 19680801      # Fixing random state for reproducibility
    randnum2 = []           # clearing list for next random set
     
    for _ in range(10):
        randnum = ((a*randnum+b) % M) / M
        randres = (vmax - vmin)*randnum + vmin    #limit to range of plot
        randnum2.append(randres)
    return randnum2
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

It actually ended up needing a break statement to force it outside of the second range before it would let me return any values. I did note the removal of the unnecessary for loop for the first few lines. Here is what I came up with:

def LCG_calc(vmin, vmax):
        
    randnum = 19680801      # Fixing random state for reproducibility
    randnum2 = []           # clearing list for next random set
    
    for x in range(0, n):
        randnum = (a*randnum+b) % M
        randnum = randnum /M
        randres = (vmax - vmin)*randnum + vmin    #limit to range of plot
        randnum2.append(randres)
        if x>n:
            break
        
    return randnum2
Apple Cola
  • 27
  • 2
  • 9
  • ‘if x>n:’ does nothing. x will never be greater than n. – quamrana Sep 24 '20 at 07:29
  • "if x>n" actually does something. When the "for x in range(0,n)" function is entered, each iteration will increment "x" by one. The statement in question will check if the function has been looped "n" times before exiting once the number of iterations (x) has surpassed "n". – Apple Cola Sep 30 '20 at 01:01
  • So you flat out contradicted me. Did you not like my reasoning? So what happens when you add a ‘print(x)’ just before the break? – quamrana Sep 30 '20 at 06:45