Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random numbers, randint
#1
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]
Reply
#2
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()
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output while using random.randint with def terickson2367 1 519 Oct-24-2023, 05:56 AM
Last Post: buran
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,725 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,530 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
Sad Iterate randint() multiple times when calling a function Jake123 2 2,061 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,299 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  Help with a random.randint choice in Python booponion 5 2,814 Oct-23-2020, 05:13 PM
Last Post: deanhystad
  output a list of random numbers 'x' columns wide adityavpratap 4 3,010 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Can i prevent the random generator to generate already used numbers? MauserMan 3 2,892 Jan-05-2020, 04:44 PM
Last Post: MauserMan
  Inheriting, given number from randint is not working beLIEve 0 1,524 Oct-12-2019, 06:50 PM
Last Post: beLIEve

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020