# How to display random numbers

**URL:** <https://discourse.processing.org/t/how-to-display-random-numbers/30298>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [May 26, 2021, 5:49am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298 "2021-05-26T05:49:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bridgette](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bridgette](https://discourse.processing.org/u/bridgette)\
**Post date:** [May 26, 2021, 5:49am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/1 "2021-05-26T05:49:42Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 26, 2021, 6:02am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/2 "2021-05-26T06:02:33Z")

</div>

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

```python
...
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.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [May 26, 2021, 6:02am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/3 "2021-05-26T06:02:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bridgette](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bridgette](https://discourse.processing.org/u/bridgette)\
**Post date:** [May 26, 2021, 6:14am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/4 "2021-05-26T06:14:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [May 26, 2021, 10:05am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/5 "2021-05-26T10:05:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bridgette](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bridgette](https://discourse.processing.org/u/bridgette)\
**Post date:** [May 26, 2021, 10:44am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/6 "2021-05-26T10:44:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [May 26, 2021, 10:45am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/7 "2021-05-26T10:45:28Z")

</div>

Can you share your code?

---

<div class="post-metadata">

**Author:** ![bridgette](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bridgette](https://discourse.processing.org/u/bridgette)\
**Post date:** [May 26, 2021, 10:57am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/8 "2021-05-26T10:57:02Z")

</div>

here you go 🙂

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 26, 2021, 11:10am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/9 "2021-05-26T11:10:17Z")

</div>

Just use the advice I gave you above –

```python
...
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)
    ...

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [May 26, 2021, 11:30am UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/10 "2021-05-26T11:30:53Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![bridgette](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bridgette](https://discourse.processing.org/u/bridgette)\
**Post date:** [May 26, 2021, 12:02pm UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/11 "2021-05-26T12:02:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [May 26, 2021, 6:20pm UTC](https://discourse.processing.org/t/how-to-display-random-numbers/30298/12 "2021-05-26T18:20:28Z")

</div>

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

```auto
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)

```
