Randomize integers

Im wanting to have a series of letters a to j and then give each one of them a random number eg, a7, b3, c6, d5, e2, etc......

then I want to write and if or else statement, if (a + b + c= 15) then print true, else print false and re_randomize, hope this is clear, im a retired engineer trying out a new hobby, so total beginner, Im looking for code as I am very limited with my knowledge , I need the letters to say in order but with a random unique number , with each loop,
Many Thanks

if (a + b + c == 15)

You will find the random() function here
https://docs.arduino.cc/language-reference/

and if you click on structure you will see the if.

If you know nothing about arduino or programming you may want to start here
https://docs.arduino.cc/learn/starting-guide/getting-started-arduino/

The letters don’t need to be ‘calculated’, they are always a-j

Can the numbers repeat? or must they be unique ?
What’s the highest number you want to see?

plan your data.
May be:

  • a mapping table to assign a number to a letter. An array might be an idea
  • hold the series of letters in another table. Again an array might be an idea.

finally you can link from one array to the other.

It might be a good idea to describe the whole project and final target you want to achieve, not only the way you want to solve the target.

how do you plan on choosing the 3 variables in the if (condition)? do you want to do that randomly as well or are there pre-determines conditions (e.g. d+h+i, a+f+h, ...)?

using an array, there can be symbols (e.g. a, b, c, ...) that are the indicies (e.g. 0, 1, 2, ..) to an array (e.g. if (var [a]+var[d]+var[g])

see Arrays, pg 23 in The C Programming Language

I want the numbers to be unique and different each time, then I want to be able to say if.
A + b + c = 15 then print true, but it may be A + B + C = 19, Then serial print false and re-randomize,

do you want to enter "a + b + c = 15" thru the serial monitor?

so the code would need to decode that text to recognize that 'a', 'b' and 'c' refer to variables with random values?

the code to decode the text could be much more complicated than the code to populate the variable array with random values and test if the sum of one or more variables equals some value

Yes that’s exactly what I’m after

google for "fisher shuffle algorithm"

2 Likes

As I alluded earlier, the letter are effectively ‘constants’
You already know their ‘names’ from their index position value…

So you only need to randomize the numbers for positions 0–9 (a-j)

Say, an array[] of ten integers (positions a-j) to hold the numbers for each round…

think about it for a while, it will come together for you.

this might get you started

// decode text equality

const byte PinLed = LED_BUILTIN;

const int N = 8;
unsigned var [N];

char s [90];

// ---------------------------------------------------------
void
randomize ()
{
    for (int n = 0; n < N; n++)
        var [n] = random (0, 100);

    Serial.println ("ready");
}

// ---------------------------------------------------------
void
disp ()
{
    for (int n = 0; n < N; n++) {
        sprintf (s, " %3d %6d", n, var [n]);
        Serial.println (s);
    }
}

// -----------------------------------------------------------------------------
void
decode (
    char *s,
    int   nChar )
{
    int  sum = 0;
    char sym;
    int  val = 0;

    for (int n = 0; n < nChar; n++)  {
        char c = s [n];

        switch (c) {
        case 'a'...'z':
            sym = c;
            break;

        case '0'...'9':
            val = 10*val + c-'0';
            break;

        case '+':
        case '=':
            sum += sym - 'a';
            break;
        }
    }

    sprintf (s, " sum %4d, val %4d   ", sum, val);
    Serial.print (s);

    if (sum == val)
        Serial.println ("true");
    else
        Serial.println ("false");

    randomize ();
}

// -----------------------------------------------------------------------------
void
loop ()
{
    if (Serial.available ())  {
        char buf [90];
        int  n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        decode (buf, n);
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    randomize ();
}

Q1; How long is this ‘series’, if not just 10?

Q2: if you are only interested in the sum of the numbers, what is the point of the letters?

That is so kind of you to send me that code, but its not quite what im after,

I want it to work out if A+B+C=15 or 18.. print true, or does D+E+F=15 or whatever then print true so serial print will say A+B+C does not equal 15, so randomise the whole thing again, if A+B+C does equal 15 then move on to D+E+F does. That equal 15 no , then randomise the whole thing again,

if (A+B+C == 15) Serial.println("true');
if (A+B+C == 18) Serial.println("equal to 18");

I should have been more specific, back in the 80’s when we were buying Sinclair spectrums I went to night classes to learn basic programming, a homework assignment was to draw a 4x 4 grid put in the numbers 1 to 16, in such an order that they add up to I think it was 34 in any direction, he wanted us to randamize the 16 numbers and place them in the grid, if the first row added up to 34 we randomised the remainder numbers, it was just pure luck if you came up with the solution, I managed to write the code and after running it for a few days I got nowhere, I wanted to see how I’d go on now using an ESP32, I’m not looking for a solution for the whole thing, I want to be challenged, I just want the code to add a+b+c+d etc on all four lines then again down all 4 columns, once I get all 4 rows I check each each column just randomising each row, hope I’ve made sense

Mick

I should have been more specific, back in the 80’s when we were buying Sinclair spectrums I went to night classes to learn basic programming, a homework assignment was to draw a 4x 4 grid put in the numbers 1 to 16, in such an order that they add up to I think it was 34 in any direction, he wanted us to randamize the 16 numbers and place them in the grid, if the first row added up to 34 we randomised the remainder numbers, it was just pure luck if you came up with the solution, I managed to write the code and after running it for a few days I got nowhere, I wanted to see how I’d go on now using an ESP32, I’m not looking for a solution for the whole thing, I want to be challenged, I just want the code to add a+b+c+d etc on all four lines then again down all 4 columns, once I get all 4 rows I check each each column just randomising each row, hope I’ve made sense

Mick

If you are just starting out in Arduino and have to learn the programming language, summing arbitrary rows and columns of a 2 dimensional grid (array) is not a particularly simple problem.

If you don't want a prepackaged solution, start with simpler tasks and work your way up. There are plenty of tutorials on line for beginners, such as this one for arrays.

?

sounds like you want to determine what any row, column or diagonal of a matrix sum up to in order to check that the #s meet some goal.

you said you wanted to create random #s, but it sounds like you want to specify values.

what exactly do you do and how do you do it?