Random number of times around a loop

Asked by alan carr

I am trying to create a loop that "loops" a random number of times between x and y

I tried the following -:

import random
random.seed()
t=random.randint(1, 5)

while True:

    for x in range(t):

### Loop ###################
        click("__CLICK-TO-CAPTURE__")
############################
Loop

I can get things to work if for "x in range(5) " which will create a 5 x loops but I require a random number of loops.

One additional point is there a way of creating a wait for image x OR y to appear

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
alan carr
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

--- random:
see: https://docs.python.org/2/library/random.html

--- wait for image x OR y
while true:
    if exists(image1, 0):
        # do something
        break
    if exists(image2, 0):
        # do something
        break
    wait(1)

Revision history for this message
alan carr (alancarr) said :
#2

I have read the python docs however I am no developer, I thought I was close to this, any more help much appreciated

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#3

import random
random.seed()

for i in range(10):
    xtimes = random.randint(1, 5)
    print xtimes
    for x in range(xtimes):
        print "x"
    print "x loop is over\n"

In output you are going to see random number of printed "x", something like this:

1
x
x loop is over

3
x
x
x
x loop is over

4
x
x
x
x
x loop is over

1
x
x loop is over

1
x
x loop is over

5
x
x
x
x
x
x loop is over

3
x
x
x
x loop is over

5
x
x
x
x
x
x loop is over

4
x
x
x
x
x loop is over

4
x
x
x
x
x loop is over

Revision history for this message
alan carr (alancarr) said :
#4

Thanks Raiman / Roman I have resolved with your help