Creating random number generator

edited April 2018 in Arduino

This is my first time ever using any programming language. I'm trying to create a small button attached to an arduino board that generates a random number from 0-100 on my computer screen while attached via USB. I looked at the reference area to try to figure out what do I write, but I am a bit lost. I could please use some help!


  • edited April 2018

    1) Can you generate a random number? Like, at all?

    2) Can you display a number? At all?

    3) Can you display the number you generated?

    4) Can you generate a new random number every time you click the mouse?

    5) Can your sketch communicate with your Arduino?

    6) Can your button communicate with Arduino?

    7) Can your sketch get some signal from the Arduino when the button is pressed?

    8) Can you regenerate the random number when that signal is received instead of using a mouse click?

    Break your problem down into smaller problems! More than likely you may be able to tackle some of them yourself... AND IF YOU CAN THEN WE DON'T NEED TO HELP YOU WITH THAT PART. So show us what you have working so far SO WE KNOW WHAT WE DON'T NEED TO HELP YOU WITH. If you don't do that, you're asking us to do the WHOLE THING for you without doing ANYTHING yourself!

  • The farthest I've seem to have gotten is simply having my arduino board and button interact with a LED. I don't know how to generate random numbers at all or how to use processing with the button.

    Here is the sketch I have so far.

    void setup() {
      pinMode(2, INPUT);
      pinMode(3, OUTPUT);
    Void loop() {
      if (digitalRead(2) == HIGH) {
       digitalWrite(3, HIGH);
    } else {
       digitalWrite(3, LOW);
  • Ok, this is the code in your arduino. Tell us where do you want to implement the random part? Have you run processing code before? Can your processing code talk to your arduino board using a serial connection?

    You can browse previous posts and find some code suitable for you:

    One possible idea is this: Your arduino sends commands via serial. You connect your arduino to your computer (physically) and in Processing, you open the serial port where your arduino is connected. Then, every time your arduino sends any data, your Processing sketch intercepts it and process it. In the processing part, you can generate a random number and displays it on the screen. So for instance, every time you push a button you get a new number in the screen.


  • Yup your idea is exactly what I’m trying to accomplish. I think I can figure out the serial connections part. It’s mostly the generating numbers where I don’t know how to do.

  • Are you trying to generate a random number on the Arduino side?

    ...or on the Processing side?

  • I am trying to get a random number generated on the processing side.

  • @metagarurumon -- great -- did the example sketch in my second link help you with that?

  • I did a bunch of trail and error these past few days. I was able to get it working from the arduino side. Thanks

  • Glad you were able to resolve your issue! If you want further feedback on your solution, share your Arduino code here.... although I am assuming that it is based on the Arduino random number example.

Sign In or Register to comment.