Pages: [1]   Go Down
Author Topic: random from a char array  (Read 1089 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
srand(analogRead(5));

Then you can select an entry from your array:

Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ... ¿?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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'.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: