Python Forum
random numbers, randint - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: random numbers, randint (/thread-41197.html)



random numbers, randint - janeik - Nov-27-2023

hello.
I got what I want in excel but it fades away so I thought I shoul try Python to do following:
Read 4 random numbers one by one, into a b c d until two conditions (a = 9 and b =20) is true.
Code attached refuse to use the logical and. It works like a logical or instead (stop running when one of conditions is achieved.

As a none repetition and using bytes (none values above 254), I have a feeling another function/methode might fit better?
Will execution of program run faster by using bytes instead of integers?

import tkinter
from random import randint
a=0
b=0
c=0
d=0
root_tk = tkinter.Tk()
window = root_tk
window.geometry("19x12+0+0")
window.minsize(19, 12)

while (a != 9) and  (b != 20):
    a=0
    b=0
    c=0
    d=0
    a = randint(1,16)
    b = randint(13,29)
    c = randint(16,34)
    d = randint(22,40)
    
print(a,b,c,d)

window.mainloop()[output][Running] python -u "d:\Python311\Scripts\randi.py"
9 26 18 31

[Done] exited with code=1 in 9.924 seconds

[Running] python -u "d:\Python311\Scripts\randi.py"
5 20 31 40

[Done] exited with code=1 in 5.363 seconds[/output]



RE: random numbers, randint - deanhystad - Nov-27-2023

It makes perfect sense to me hat or works. The and loops stops if either comparison is false. True and True == True, True and False == False, False and True == False, False and False == False. SO if a == 9 or if b == 20, the loop stops.

Or works because the only way for the loop to end is for both comparisons to be False: True or True == True, True or False == True, False or True == True, False or False == False. The only way for the loop to end with an or is for a != 9 to be False (a == 9) and b != 20 to be False (b == 20)

Maybe you'll like this better:
while not (a == 9 and b == 20):
What is the purpose of this code. It is really strange. Why not set a = 9 and b = 20 and c and d to random numbers?

There is no reason to initialize a, b, c and d to zero.

Why are you using tkinter? If you want a window to pop up and display the results, all you need is an input statement.
from random import randint

while True:
    a = randint(1,16)
    b = randint(13,29)
    c = randint(16,34)
    d = randint(22,40)
    if a == 9 and b == 20:
        break
print(a, b, c, d)
input()



RE: random numbers, randint, SOLVED - janeik - Nov-27-2023

(Nov-27-2023, 01:07 PM)deanhystad Wrote: It makes perfect sense to me hat or works. The and loops stops if either comparison is false. True and True == True, True and False == False, False and True == False, False and False == False. SO if a == 9 or if b == 20, the loop stops.

Or works because the only way for the loop to end is for both comparisons to be False: True or True == True, True or False == True, False or True == True, False or False == False. The only way for the loop to end with an or is for a != 9 to be False (a == 9) and b != 20 to be False (b == 20)

Maybe you'll like this better:
while not (a == 9 and b == 20):
What is the purpose of this code. It is really strange. Why not set a = 9 and b = 20 and c and d to random numbers?

There is no reason to initialize a, b, c and d to zero.

Why are you using tkinter? If you want a window to pop up and display the results, all you need is an input statement.
from random import randint

while True:
    a = randint(1,16)
    b = randint(13,29)
    c = randint(16,34)
    d = randint(22,40)
    if a == 9 and b == 20:
        break
print(a, b, c, d)
input()

Hello and thanks for reply.
The numbers given for a,b,c,d (i.e. 1-16) are mean numbers combined with standard deviations for each number, to define areas (swing) (i.e.: 1-16,13-29...)

Numbers i.e. 9,20 are part of lottery draw.