How to GOTO a certain line in Python to repeat?

In summary: I can't seem to find a way to make the code work for i=1 to 10 and 100 to 110.In summary, the code does not work.
  • #1
Othman0111
27
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');
 
Technology news on Phys.org
  • #2
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)
 
  • #3
.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"
 
  • #4
Correct: Line 11 is Line 17 where it says
a=100*i (the new I from the loob)
 
  • #5
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:
  • #6
.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.
 
  • #7
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');
 

1. How do I use the GOTO function in Python?

The GOTO function is not available in Python. It was removed from the language due to its potential for creating confusing and difficult-to-understand code. Instead, it is recommended to use other control structures such as loops and functions.

2. What is the alternative to GOTO in Python?

The alternative to GOTO in Python is using other control structures such as loops and functions. Loops allow you to repeat a set of instructions multiple times, while functions allow you to group a set of instructions and execute them whenever needed.

3. How can I repeat a certain line of code in Python?

To repeat a certain line of code in Python, you can use a loop. For example, a for loop can be used to repeat a specific line of code a set number of times, while a while loop can be used to repeat a line of code as long as a certain condition is met.

4. Can I use GOTO for debugging purposes?

No, it is not recommended to use GOTO for debugging purposes. Instead, you can use debugging tools such as breakpoints and print statements to track the execution of your code and identify any errors or bugs.

5. Is GOTO used in any other programming languages?

Yes, GOTO is used in some other programming languages such as BASIC and FORTRAN. However, it is not commonly used and is often discouraged due to its potential for creating confusing and difficult-to-understand code.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
895
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top