Random numbers to char array??

Hello all,

This is my first time ever posting a question, usually I go on a google/forum search to find my answer (usually works). But, I've come across an issue where I cannot find or I am just not understanding.

What I am trying to do is have a set of 10 randomize numbers which will be later used for a "Phone Number", so it will need to be within a char array. I've attempted to find an answer on how to convert/extrapolate the 10 digits from an int array so that it can be used for a char array, but to no avail. I also thought/looked into possibly working with String, since it seems a bit versatile. Again, I'm not sure how to incorporate the 10 digit random numbers ranging from 0-9.

Thanks for any advice, guidance, or information.

Cheers from Afghanistan!

This will fill an array with random numbers

  char number[10]
  for(int i = 0; i<10; i++){
   number[i]= random(0,10); 
  }

Or if you want the ASCII value of these numbers then

  char number[10]
  for(int i = 0; i<10; i++){
   number[i]= 0x30 | random(0,10); 
  }

I guess you have a char array or String to hold the ten digits. (Personally I’d recommend the char array to avoid using the problematic String class.)

You can generate a random number in the range 0 … 9 by calling random(10). Note that the returned values will not be truly random unless you first call randomSeed() with a random value such as the result of analogRead() on an unconnected pin.

You would generate one random number for each digit you want. A for loop is the obvious way to do that.
Each random number needs to be converted to an ascii character in the char array or String. There are lots of different ways to do that but the simplest takes advantage of the fact that the ascii codes for the digits 0 … 9 are consecutive - if you just add your number (in the range 0 … 9) to the ascii code for ‘0’ you get the ascii code for the corresponding digit. e.g. ‘0’ + 3 gives you ‘3’.

const int length = 10;
char result[length+1]; // +1 allows space for the null terminator
for (int i = 0; i < length; i++)
{
    result[i] = '0' + random(10);
}
result[length] = '\0'; // add the null terminator
Serial.println(result);

Something like this ???

char ph_num[10];
int digit[10];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // generate 10 random numbers range 0..9
  for (byte i=0; i<10; i++)  digit[i] = random(10);
  // move digits into char array
  for (byte i=0; i<10; i++)  ph_num[i] = char(48+digit[i]);
  // print phone number
  Serial.println(ph_num);
  delay(1000);
}

Thanks for the speedy response, you guys!

Oddly enough, that was the first thing I tried. When I pulled up the Serial Monitor, all I was getting were weird symbols (well if anything displayed in the first place). There was no extra code, just simply a test snippet to see if char would receive random numbers. I'll give it another careful shot.

Seriously, thanks again!

Ok so I tested out what I did before:

char number[10]
  for(int i = 0; i<10; i++){
   number[i]= random(0,10); 
  }

with the same results that was driving me nuts,alot of random symbols being displayed.

But the snippet Peter provided it worked perfectly… The ASCII conversion is probably what help? Sorry for the bother, I’m sure if I had read more into what I want to use I wouldn’t run into many issues.

Thanks/Takk guys

with the same results that was driving me nuts,alot of random symbols being displayed.

Yes that is why I gave you two pieces of code, one with numbers and one with ASCII characters as you did not say what you wanted.

If you check out the ASCII entry on Wikipedia, it will explain why the first example produced random garbage, while the one that adds 48 to everything produces actual numbers.

The characters displayed on your screen are actually mapped to different numbers in the ASCII character set. Numbers less than 31 are used for controlling data streams, and contain things like the null character '\0', carriage returns '\r', newlines '\n' and other stuff. The characters that actually print numbers start at an integer value of 48 for '0', which in C++ is not the same as 0.

PeterH:
You can generate a random number in the range 0 .. 9 by calling random(10). Note that the returned values will not be truly random unless you first call randomSeed() with a random value such as the result of analogRead() on an unconnected pin.

May not be important to the OP's need, but what you describe would not be truly random. Indeed it would results in quite predictable results.

Youssa:
What I am trying to do is have a set of 10 randomize numbers which will be later used for a "Phone Number"...

Are you building an auto-dialer?

wanderson:
May not be important to the OP’s need, but what you describe would not be truly random. Indeed it would results in quite predictable results.

The randomness comes from the result of the analogRead() on a disconnected pin. I guess we could debate whether that’s truly random, but it’s certainly inconsistent and unpredictable. It’s only if you fail to seed the random generator, or seed it with a predictable value, that the subsequent random numbers are predictable.

PeterH:
I guess we could debate whether that's truly random...

That would be a very short debate. It is not.

...but it's certainly inconsistent and unpredictable.

In my testing, it is not. Even when the pin is floating / the analogValue is changing, the values tend to be highly clustered around about a half-dozen of the potential values. In other words, from the human eyeball perspective the values appear to be inconsistent and unpredictable but applying just a bit of math reveals that using a floating pin to initialize a random number generator is a bad idea.

While not directly on topic, I can’t even begin to tell you how many students have answered random number generator questions with something like:

void setup() { 
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 

  const int length = 10;
  char result[length+1]; // +1 allows space for the null terminator

  for (int k = 0; k < 10; k++) {
    randomSeed(10);
    for (int j = 0; j < 10; j++) {
      for (int i = 0; i < length; i++)
      {
        result[i] = '0' + random(10);
      }
      result[length] = '\0'; // add the null terminator
      Serial.print("  ");
      Serial.print(result);
    }
    Serial.println("  ");
  }
}
void loop(){
}

They believe that using the randomSeed() function generates a sequence of randomly-distributed numbers. What it does produce is a repeatable sequence of randomly-distributed numbers. Big difference.