Mazze generator

I am trying to make a Mazze generator using / and \ but I can't use them so I have to use ASCII values 47 and 92. I don't know how to use a random for two specific numbers. this is what I have so far it only prints /.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
long randNumber;
int slash[2] = {47, 92};

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);


void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);

  Serial.println("128x64 OLED FeatherWing test");
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default

  display.display();
  delay(1000);

  display.clearDisplay();
  display.display();
  display.setRotation(1);
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0,0);

}

void loop() {
  // put your main code here, to run repeatedly:
  randNumber = random(0, 1);
  display.write(slash[randNumber]);
  delay(10);
  display.display();
}

From the random reference page:

Syntax
random(max)
random(min, max)

Parameters
min: lower bound of the random value, inclusive (optional).
max: upper bound of the random value, exclusive.

Returns
A random number between min and max-1. Data type: long.

You're asking for a random number between 0 and 1-1.

Also:

char slash[2] = {'/', '\\'};
randNumber = random(0, 1);

What range of random numbers will this return ?

Reading https://docs.arduino.cc/language-reference/en/functions/random-numbers/random/ might help answer the question

You can use them like this

char slash[] = { '/', '\\' };

void setup()
{
    Serial.begin(115200);
    Serial.println(slash[0]);
    Serial.println(slash[1]);
}

void loop()
{
}

I was hopeing 1 and 0 in random order.

If you didn't look at the link to the reference page for random that I provided, did you read the excerpt from it that I included?

You may have been hoping for a random number in [0, 1], but your code is asking for a random number in [0, 1).

The solution should be obvious.

I am trying to generate a random order of back and forward slashes onto an OLED screen. I don't know how to create a random order.

Start by generating a random number that is either 0 or 1

randNumber = random(0, 1);

Does not do it

You've been told what you've done wrong. Three times now, in fact. If you won't read, if you won't think, there's nothing more anyone can do to change that.

Ok, I sat down for a second look. It was as simple as changing it to 2. It also gave me some more info on how to make it more random.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define i2c_Address 0x3c
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
long randNumber;
char slash[2] = {'/', '\\'};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println("128x64 OLED FeatherWing test");
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default
  display.setRotation(1);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0,0);
  display.display();
  randomSeed(analogRead(A0));
}

void loop() {
  // put your main code here, to run repeatedly:
  randNumber = random(2);
  display.write(slash[randNumber]);
  delay(10);
  display.display();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.