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
Click For Summary

Discussion Overview

The discussion revolves around generating a specific sequence of numbers in Python to evaluate a random number generation scheme. Participants explore alternatives to using a GOTO statement, which is not available in Python, to repeat certain lines of code while implementing a sequence that skips a defined range of numbers.

Discussion Character

  • Homework-related, Technical explanation, Exploratory

Main Points Raised

  • One participant expresses a need for a GOTO function to repeat a line of code for generating a sequence of numbers, specifically evaluating r for certain ranges.
  • Another participant suggests a nested loop structure to achieve the desired sequence of numbers, clarifying that a GOTO is not necessary.
  • There is a repeated emphasis on generating a sequence that includes numbers from 1 to 10, then skips 90 numbers, and continues with the next set of ten.
  • One participant proposes a code snippet that uses a variable to store the last generated random number, iterating through the sequence while updating the random number based on a formula.
  • A later reply indicates that the professor provided feedback, suggesting that the skipped numbers should still be generated but not included in the output.
  • Another participant shares a code example that incorporates the professor's feedback, adjusting the logic to account for the skipped numbers while still generating them internally.

Areas of Agreement / Disagreement

Participants generally agree on the need to generate a sequence with specific characteristics, but there is some uncertainty regarding the implementation details and the necessity of generating skipped numbers. The discussion reflects multiple approaches and interpretations of the problem without a clear consensus on the best method.

Contextual Notes

Some limitations in the discussion include unclear definitions of the sequence requirements and the handling of skipped numbers, as well as unresolved mathematical steps in the proposed solutions.

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');
 
Technology news 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');
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K