How to GOTO a certain line in Python to repeat?

  • Context: Python 
  • Thread starter Thread starter Othman0111
  • Start date Start date
  • Tags Tags
    Line Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 4K views
Othman0111
Messages
27
Reaction score
0
Hi everyone,
I'm trying to create a sequence that evaluates r for i= 1 to 10 and 100 to 110 ...N (evaluate 10 skip 90 repeats till N).
If python has a GoTo function, I would've used it to return the previous line to repeat. But I couldn't find a function like that anywhere on the internet.

1. Homework Statement

Martin Lüscher’s scheme to fix poor random-number generators is to generate a small block of random deviates, then “throw away” a large block, causing the generator to “forget” about the small block and iterate. For example, you can generate and store 10 values, then generate and discard 90 values, then generate and store another 10 values, etc. Try this quick fix in the examples above and see if things improve. The experiment also with different lengths of the short/long blocks you store/discard. Comment and document your findings.

Homework Equations



Σa(a+10) r(i)
a=100i
where i = 0,1,2,3,4,...N

The Attempt at a Solution



Python:
%matplotlib inline
import numpy as np
from matplotlib import pyplot
import itertools as it

N = 5002
r = np.zeros(N+1)
M = np.zeros(N+1)
j = np.zeros(N+1)

coef = 523612312121
bigint = 65379170218971
i=0
a=100*i
M=100*i+10
seed = 1.
r[0] = seed
for i in range(a,M):
    r[i] = (coef*r[i-1])%bigint
for i in it.repeat(r[i], 10):
    if i<=N: #GO TO LINE 11

   
   
       
       
r1 = np.zeros(N//2)
r2 = np.zeros(N//2)
for i in range(0,N,2):
    r1[i//2] = float(r[i])/float(bigint)
    r2[i//2] = float(r[i+1])/float(bigint)
   
pyplot.plot(r1,r2,marker='o',linestyle='None');
 
on Phys.org
Not only are you thinking about using a goto, you're thinking of breaking out of a loop with it.
Line 11 is the coef assignment - and I don't think you really want to jump to that spot. So I am unclear on exactly what you are thinking.

That said, here's what you want to do to get the 1..10, 101..110, 201..210 ... N sequence:
Python:
for Ia in range(1,N-10,100):
  for Ib in range(10):
    I=Ia+Ib
    print(I)
 
.Scott said:
Not only are you thinking about using a goto, you're thinking of breaking out of a loop with it.
Line 11 is the coef assignment - and I don't think you really want to jump to that spot. So I am unclear on exactly what you are thinking.

That said, here's what you want to do to get the 1..10, 101..110, 201..210 ... N sequence:
Python:
for Ia in range(1,N-10,100):
  for Ib in range(10):
    I=Ia+Ib
    print(I)

the sequence I'm trying to generate is 1,2,3,4,5,6,7,8,9,10, Skip (90), 101,102,103,104,105,106,107,108,109,110, Skip(90), ...N
but why I'm trying to do that?
I have a function look like this
r(i)=(C*r(i-1))%B I need to plot a random 2d distribution based on the remainder of this formula. However I can't use i = 1,2,3,...N, I have to use the said sequence. To make it "more random"
 
Correct: Line 11 is Line 17 where it says
a=100*i (the new I from the loob)
 
Othman0111 said:
the sequence I'm trying to generate is 1,2,3,4,5,6,7,8,9,10, Skip (90), 101,102,103,104,105,106,107,108,109,110, Skip(90), ...N
but why I'm trying to do that?
I have a function look like this
r(i)=(C*r(i-1))%B
Okay. I'm still not fully clear on what you want to do - or whether my last post answered your question.
Perhaps this:
Python:
lastR = seed
for Ia in range(1,N-10,100):
  for Ib in range(10):
    I=Ia+Ib
    r[I]=(coef*lastR)%bigint
    lastR = r[I]
If this isn't what you want and you still can't work it out yourself, please say so explicitly.
 
Last edited:
.Scott said:
Okay. I'm still not fully clear on what you want to do - or whether my last post answered your question.
Perhaps this:
Python:
lastR = seed
for Ia in range(1,N-10,100):
  for Ib in range(10):
    I=Ia+Ib
    r[I]=(coef*lastR)%bigint
    lastR = r[I]
If this isn't what you want and you still can't work it out yourself, please say so explicitly.
I did that and the code works fine. I show it to the professor today and he said: you skipped counting the 90 digit, you supposed to generate them but not included them into the output.

Thanks for the help mr. Scott. I didn’t understand the question from the beginning.
 
The professor says this is the correct answer:

Python:
%matplotlib inline
import numpy as np
from matplotlib import pyplot

N = 50002
r = np.zeros(N+1)

coef = 52361276312121
bigint = 6537169170218971

# A poor choice
coef = 112
bigint=2555seed = 1.
r[0] = seed
for i in range(1,N+1):
    if (i-1)%10==0:
        r[i] = (coef*r[0])%bigint
    else:
        r[i] = (coef*r[i-1])%bigint
    if i%10==0:
        r[0]=r[i]
        for j in range(1,1):
            r[i]=(coef*r[i])%bigint
        r[0],r[i]=r[i],r[0]
                   
       
r1 = np.zeros(N//2)
r2 = np.zeros(N//2)
for i in range(0,N,2):
    r1[i//2] = float(r[i])/float(bigint)
    r2[i//2] = float(r[i+1])/float(bigint)
   
pyplot.plot(r1,r2,marker='o',linestyle='None');