How to display random numbers

Hello,

I’m trying to figure out how to create random numbers that displays on the screen. My code is animated, so the text moves as well and I’m not sure how to stop it. Does anyone know how? Could I use Boolean somehow?

def mouseClicked(): 
  textSize(20)
  text(random(20), 100, 200)

Your text will get drawn over immediately. You can use a structure like this:

...
r = 0

def draw():
    background(128)
    text(r, 100, frameCount)

def mouseClicked():
    global r
    r = random(20)

I’ve used the frameCount to add some movement on the y-axis.

2 Likes

first, you need to store the random value in a varaible. After that just display the variable

I tried implementing that into my code, now the numbers don’t show up at all. Is this because of def mouseClicked() ?

mouseClicked() only triggers once you press the mouse. You have to use text(txt,x,y) in loop, otherwise it will only show up once you press the button and will disappear right afterwards.

but the issue is, even when the mouse is clicked, nothing would show. so is there any way to fix that?

Can you share your code?

here you go :slight_smile:

w, h = 550, 700

pfc = (10, 25, 225)
bgc = (225, 225, 225)

pw = 3
rw = 3

def mouseClicked(): 
  textSize(20)
  text(random(20), 100, 200)

def setup():
    size(w, h)
    frameRate(1)
    pixelDensity(2)

def draw():
    background(bgc[0], bgc[1], bgc[2])
    for i in range(1000):
      point( random(width), random(height) )
    planet((width/2, height/2), 200)
    
  for y in range(height):
    for x in range(width):
      distanceFromCenter = dist(x, y, width/2, height/2)
      stroke(distanceFromCenter)
      point(x, y)

def planet(position, ps):
    translate(position[0], position[1])
    starting_height = random(ps/4, ps/2)
    starting_width = random(ps*1.2, ps*2)
    
    rotate(random(2.7*PI))
    
    fill(pfc[0], pfc[1], pfc[2])
    strokeWeight(pw)
    circle(0, 0, ps)
    
    noFill()
    strokeWeight(rw)
    for i in range(int(random(3, 9))):
        ellipse(0, 0, starting_height + i * 10, starting_width + i * 30)
        
    strokeWeight(pw)
    fill(pfc[0], pfc[1], pfc[2])
    arc(0, 0, ps, ps, 1.5*PI, 2.5*PI)

Just use the advice I gave you above –

...
r = 0  # add this line

def mouseClicked(): 
    # comment out these two lines:
    # textSize(20)
    # text(random(20), 100, 200)
    # add these two lines:
    global r
    r = random(20)

...

def draw():
    background(bgc[0], bgc[1], bgc[2])
    # add these two lines:
    textSize(20)
    text(r, 100, frameCount)
    ...
n = 0
def mouseClicked():
    global n
    n = random(100)
def setup():
    size(600,600)
    global n
def draw():
    background(0)
    text(n,300,300)
    

if i change def mouseClicked to define a function of my own (like def serialNumber) would the format still work?

Just a side note, you have to set n as a global variable to be able to change it outside when it is defined

n = 0
def mouseClicked():
    serialNumber()
def serialNumber():
    global n
    n = random(100)
def setup():
    size(600,600)
    global n
def draw():
    background(0)
    text(n,300,300)