Go Down

Topic: random from a char array (Read 1 time) previous topic - next topic

rdiaz

Hi, I made an array like this: a[17] = {'0','1', ...'8','9','A','B'...'F'}, and now I want to send one caracter at the time through serial by choosing a random character from this array, how can I do this?

majenko

You want to use the rand() function to generate a random number.

You will also want to "seed" the random number generator with a random number (chicken...egg...?).

The "srand" function seeds the random number generator:

Code: [Select]

srand(analogRead(5));


Then you can select an entry from your array:

Code: [Select]

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


The "%17" is "Modulus 17".  This limits the results of rand() (which returns numbers in the range 0 to  65535) to between 0 and 16.  It's basically the "remainder" of dividing by 17.

rdiaz

Ok, I get what you say, but what about the letters from 'A' to 'F' ? ... maybe I wasn't clear enough...my bad!  :smiley-eek-blue:... I just wanted to send characters from '0' to '9' and from 'A' to 'F', which are already in an array. Using your way I think I can only send numbers ... ¿?

rdiaz

mmm...well now I understand what you meant... forget the last reply, I'll try what you just said, thanks!! =)

majenko

Code: [Select]

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


This line first generates a random number between 0 and 16 (rand()%17).  It then uses this random number to select one of the entries from the character array.  This entry is then assigned to the character variable "entry".

So, for example, if the random number generator gives us a value of "12", it will select entry number 13 (counting from 0), which will, I guess, be 'D'.

majenko

Actually, if you're using 0-9 & A-F (basically hexadecimal), then that's only 16 items.  Your array is set up for 17.  You might want to change all the 17's to 16's to avoid problems; if it selects entry 17 (number 16) it will return, probably, ASCII character 0 - or it may not - it depends what is in that bit of memory.

rdiaz

About the 'analogserial', is it necesary to have an analog input? or it's just how the instruction must be written? ... and about the size of the array, well you're right, I thought about it , but it won't be a problem compiling the program if I resize that array to 16?

majenko

I'm not sure what you mean by "analogserial".  Are you referring to the srand(analogRead(5)); line?  If so, that makes the random numbers more random.  Without it the "random" numbers will always come in the same sequence.  The srand() function takes a number that should be itself random.  The best way to get a number to pass to it is to read an analogue input that isn't connected to anything.

And as or resizing the array to 16 - if there's 16 entries in it, then what do you need the 17th entry for?  If you were using it to store a string, then you'd want 1 extra entry for the 0 at the end of the string, but for just an array of values there is no 0 at the end.

rdiaz

Ok, I'm gonna try sending some characters! ...thanks!

Go Up