storing array for randomseed

Hi guys,

Needing some help here.

I realized that randNumber that was declared have to be an integer.

Yet in order for me to store the random number generated, I have to use Char.

However, I'm unable to store them.

my serial.printLn (numbuf) returns nothing.

Thanks for taking the time.

char numbuf[9];

void password(char buf[])
{
  uint8_t i, a;
  randomSeed(analogRead(0));
  int randNumber;
  Serial.println("Generating Password");

  for( i=0; i < 8; i++){

    while(i != 8)
    {
      randNumber = random(10);    // Print random numbers with no seed value

      numbuf[i]= randNumber; 
      
      i++;
      mySerial.print(randNumber);
      Serial.println(randNumber);

    }

    Serial.println(numbuf);
  }

  
}

Type char is a signed 8 bit integer. It can be -128 to +127.
Type int is a signed 16 bit integer.

In order to store an int to a char when you know the int has a value that can fit in the char you must.

char  A;
int     B = 7;

A = (char) B; // this is known as casting an int to a char

It's how you tell the compiler to not just grab the high 8 bits of the int to fill the 8 bits of the char.

Hi,

Thanks for the quick reply. Do bear with me here.

I tried using the above code you mention replacing variables to fit my situation. I'm still unable to store it.

Do have a look at my current code.
Uploading is fine.

char numbuf[9];                 //set array to having 8 + 1 digit


void password(char buf[])
{

  uint8_t i, a;
  randomSeed(analogRead(0));
  int randNumber;
  

  Serial.println("Generating Password");

  for( i=0; i < 8; i++){

    while(i != 8)
    {
      randNumber = random(10);    // Print random numbers with no seed value


      buf[i] = (char) randNumber; // this is known as casting an int to a char



        i++;
      mySerial.print(randNumber);
      Serial.println(randNumber);

    }

    Serial.println(numbuf);
  }


}
char numbuf[9];                 //set array to having 8 + 1 digit


void password(char buf[])
{

  uint8_t i, a;  // uint8_t takes longer to think than 'byte' which is the same thing. 
  randomSeed(analogRead(0));
  int randNumber; 
  

  Serial.println("Generating Password");

  for( i=0; i < 8; i++){

    while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out
    {
      randNumber = random(10);    // Print random numbers with no seed value

    // don't you mean numbuf, not buf? otherwise declare buf[] 
      buf[i] = (char) randNumber; // this is known as casting an int to a char



        i++;
      mySerial.print(randNumber);
      Serial.println(randNumber);

    }

    Serial.println(numbuf); // and here's your extra print
  }


}
  for( i=0; i < 8; i++){

    while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out

The compiler doesn't optimize it out. The for loop index is from i=0 to i<8 which means that 'i' will take on the successive values 0, 1, 2 .. 6, 7.
Why is there a while loop inside the for loop??

Pete

el_supremo:

  for( i=0; i < 8; i++){

while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out



The compiler doesn't optimize it out. The for loop index is from i=0 to i<8 which means that 'i' will take on the successive values 0, 1, 2 .. 6, 7.
Why is there a while loop inside the for loop??

Pete

Also note that i never changes in the while loop. It's an infinite loop.

It does change. There's an i++ in there.

Pete

I have removed the while loop.

char numbuf[9];                 //set array to having 8 + 1 digit


void password(char buf[])
{


  {
    byte i,a;

    Serial.println("Generating Password");

    randomSeed(analogRead(0));
    int randNumber; 



    for(i=0; i<=7; i++){    //allows only 8 Digit for Password
      randNumber = random(10);
      a = randNumber;

      mySerial.print(randNumber);
      Serial.println(randNumber);




      numbuf[i] = (char) randNumber; // this is known as casting an int to a char 


    }

    Serial.println(numbuf);
    buf[i]=0;                 // reset my password
  }

}

Whats' shown in my serial monitor:
Generating Password
8
6
3
4
5
5
6
7
(Serial.Print numbuf) to appear here but it doesnt.

Sending Password

It appears that it is not storing in as when I did my Serial.println(numbuf);

Nothing is displayed here and my code moves along.

GoForSmoke, I have changed buf to numbuf*. Same thing happens as well.*

el_supremo:
It does change. There's an i++ in there.

Pete

So there is, I was wrong..

and then the for next increments i as well. The extra line with only i++; needs to go.

Try this

char numbuf[9];                 //set array to having 8 + 1 digit

void setup()
{
  Serial.begin(115200);
  password(); 
}

void loop()
{
}

void password()
{
  {
    Serial.println("Generating Password");
    randomSeed(analogRead(0));
    int randNumber; 

    for(int i=0; i<=7; i++)
    {
      randNumber = random(10);
      Serial.println(randNumber);
      numbuf[i] = (char)(48 + randNumber);
    }
    numbuf[8] = '\0';
    Serial.println(numbuf);
  }
}

Thanks. These work like a charm.

Just to enhanced my programming knowledge.

What does (48 + randNumber) does?

Does( numbuf[8] = '\0'; ) resets my numbuf array?

The ASCII code for 1 as a char is 49. So, if the random number generated is a 1 we need to add 48 to it to get its char value.

ASCII 2 is 50, 3 is 51 etc. See http://www.asciitable.com/ So, adding 48 to an integer between 0 and 9 gives its value as a char.

Adding '\0' to the end of the array ensures that the char array is seen as a string and can be used as such. It does not 'reset' the array, but why do you need to because next time you put 8 chars in it they will overwrite the ones already there.

char zeroToNine = 1;
char printableNumber = '0' + zeroToNine;

printableNumber is now 49, ASCII '1'
We can code ASCII values by putting the character in single quotes, and it's an 8 bit signed integer that prints that character in the quotes and can be operated on with math. 'A' + 3 == 'D' just as '0' + 1 == '1'. And the difference between uppercase and lowercase alphas is the lowercase have bit 5 set, 'a' - 'A' == 32.