# Re: random from a char array

Then how to limit the generating output ?
the output only gives a number or letter
what about up to 5 letters and numbers ouput?

i've seen 1 post here that generate numbers and letters.
but it display only 1 letters or number.
how to put limitation on the output?

long entry;
int a[14] = {'1','2','3','4','5','6','7','8','9','0','A','B','C','D'};
void setup(){
Serial.begin(9600);

}
void loop() {

entry = a[rand()%14];
Serial.println(entry);

delay(300);
}

Programming is all about making things from building blocks.

You'd create a method randomChar. Then you'd call it 5 times. See if you can write a method randomChar, and a method randomCharArray. Make the randomCharArray take a pointer to an array and a size, and fill the array with repeated calls to randomChar. This is a useful learning exercise.

aspirines:
Then how to limit the generating output ?
the output only gives a number or letter
what about up to 5 letters and numbers ouput?

This thread is very old, I don't know if the user that originally answer this is still using the forum. So, I will try to answer that.

Then how to limit the generating output ?

Changing the 17 in the line:

``````char entry = a[rand()%17];
``````

the output only gives a number or letter
what about up to 5 letters and numbers ouput?

In this case, the idea was to generate all the numbers, and the letters from A to F. If you want different numbers or letters you only need to change the array content.

The variable `entry` can't be `long`. It must be `char`.

how to put limitation on the output?

I don't understand what do you mean with "limitation".

i mean, limit the output up to 5.
it only generate an ouput up to 1

so it should be like this.

serial monitor:

A23B5

i know a little bit about the array but my knowledge is still not broad about arduino.

Now I understand. Try this:

``````char entry;
char a[14] = {'1','2','3','4','5','6','7','8','9','0','A','B','C','D'};
void setup(){
Serial.begin(9600);

}

void loop() {

for (int i=0; i<5; i++) {
entry = a[rand()%14];
Serial.println(entry);
}
delay(300);
}
``````

But maybe you don't are telling everything. If you only want to generate a `long` number, you can use a different approach.

i missed the increment, wow, really thanks luisilva.

Fell free to hit the +karma button:

<<<<<<HERE.

``````char a[] = {'0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F'};

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

void loop() {
long rand;

rand = random(0, 17);
Serial.print("rand = ");
Serial.print(rand);
Serial.print("   char = ");
Serial.println(a[rand]);

}
``````

You could avoid the array altogether and save a few bytes of RAM.

``````char GetHexVal( const char in = random( 0, 15 ) ){
switch( in ){
case 0 ... 9: return '0' + in;
case 10 ... 15: return 'A' + ( in - 10 );
}
return 0;
}

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

void loop() {
Serial.println( GetHexVal() );
delay( 500 );
}
``````

same Homework Assignment?

@aspirines, do not cross-post. Do not hijack. Threads split and merged.

how will it display as a bundle ?

all codes given are excellent and all of them is what i need.
and how can i display it as "AB23D"

as i tried it showed one by one,
A
B
2
3
D

how about like this: "AB23D" //shows as a group.

sorry for that sir Coding Badly.

as i tried it showed one by one,

Using what code? It looks like you are using println() where you want to be using print().

i've got 5 outputs "xxxxx"

``````void loop() {
// print a random number from 0 to 299
randNumber = random(11111,99999);     //i didn't know i can get an output of 5 numbers at the same time if i put this :D .
Serial.println(randNumber);
delay(50);
}
``````

it would be big help if you can help me display 5 output combined with letters and numbers. it would benefit also to future researchers

PaulS:
Using what code? It looks like you are using println() where you want to be using print().

Sir paulS, i'm also trying to display it in LCD but the display would show like
this: =A=B=2=3=D which the delay shows up also.

i tried experimenting:

long randNumber;

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

void loop(){
randNumber = random(11111,99999); //in which i can get the output of 5 numbers, this is what i needed output
Serial.println(randNumber);

delay(50);
}

i would really be glad if you can help me with this, an output combined with letter and number.

The modification is simple:

``````int i = 1;         // Add this just before GetHexVal()

// rest of code...

void loop() {

Serial.print( GetHexVal() );   // Note we're using the print() method, not println()
if (i++ % 5 == 0) {
Serial.println();            // Now use println()
delay( 500 );
}

}
``````

got it Sir econjack, thank you.

also to those who give me idea,