openlang

Why is B appearing so often with this randomizer?

This program's randomizer repeatedly produces lengthy runs of B's. Can I do anything to get a broader mix of results?


import random
import pyglet
#opens window
screen = pyglet.window.Window(fullscreen=True)
caption = pyglet.text.Label('press A',
                          font_name='Times New Roman',
                          font_size=72,
                          x=screen.width//2, y=screen.height//2,
                          anchor_x='center', anchor_y='center')
alternate= pyglet.text.Label('press B',
                          font_name='Times New Roman',
                          font_size=72,
                          x=screen.width//2, y=screen.height//2,
                          anchor_x='center', anchor_y='center')
def refresh(elapsed):
    pick=random.SystemRandom().randint(1,2)
    if pick==1:
        screen.clear()
        caption.draw()
    else:
        screen.clear()
        alternate.draw()
pyglet.clock.schedule_interval(refresh,pick)
pyglet.app.run()

The conversation continues 2

Follow the reasoning
  1. Permalink

    I see greater variation with a shorter interval - for instance, 0.2

    
    pyglet.clock.schedule_interval(refresh, 0.2)

    since each label stays visible less time.

    Your pick in schedule_interval() has a problem - you get NameError: name 'pick' is not defined since pick exists locally within refresh() and isn't available beyond that function.

  2. Permalink

    This example won't run since the variable pick isn't in the scope containing the invocation of pyglet.clock.schedule_interval

    What you intend isn't clear. Should the interval really differ from one execution of this program to another? Keeping the interval constant seems more reasonable.

    Reorganizing things could help.

    The runtime behavior seems a little strange, although that could simply be something about pyglet or my limited understanding of it. Regardless, this alternates between showing "Press A" and "Press B" at a pseudo-random rate.

    
    from random import SystemRandom
    from pyglet import window, text, clock, app
    
    # pylint: disable=possibly-used-before-assignment,unused-argument
    
    # note use of variable names in uppercase denoting global scope
    
    TICK_SECONDS = 3.
    
    def refresh(elapsed: float) -> None:
        SCREEN.clear()
        RNG.choice(CAPTIONS).draw()
    
    if __name__ == "__main__":
        SCREEN = window.Window(fullscreen=True)
        label_options = {  # these parameters are common to both captions
            "font_name": "Times New Roman",
            "font_size": 72,
            "x": SCREEN.width // 2,
            "y": SCREEN.height // 2,
            "anchor_x": "center",
            "anchor_y": "center",
        }
        # create a tuple of the 2 captions
        CAPTIONS = text.Label("Press A", **label_options), text.Label("Press B", **label_options)
        RNG = SystemRandom()  # only do this once
        clock.schedule_interval(refresh, TICK_SECONDS)
        app.run()

Other sides of the same question

  1. Making the sender appear as a label instead of an email address using Java Mail

    Im sending messages through the Java Mail API. On arrival, the inbox displays the address that sent the message. Id like some…

    Replies 2Latest activity java
  2. Dynamics CRM access from TypeScript

    Because my CRM code runs in a webresource, I have to write to retrieve the entity's data. Regrettably, that expression fails…

    Replies 2Latest activity typescript
  3. What date formatting does a Gleam HTTP response header need?

    I want to send a Date header. The required format looks like this: This is my current attempt:

    Replies 3Latest activity gleam