Showing posts with label random(). Show all posts
Showing posts with label random(). Show all posts

March 6, 2011

Random Arduino Bits 'n Bytes (part 2)

(This is a follow-up article. Read the first part here.)

Now the bit-level manipulation part of the code:

/*
 * randoms_seed.pde
 * -----------------
 * Generates a random number, then prints it
 * through the serial port in decimal and
 * binary format. Does not use an initial seed
 * so it prints the same set of random numbers
 * everytime it is reset.
 *
 *
 * http://spacetinkerer.blogspot.com
 */

long randNumber;


Random Arduino Bits 'n Bytes (part 1)

Let's take a look at the random number generation on arduino, and on bit-level manipulation. This lesson will be helpful for a project I'm preparing.

So arduino has the random() function that when called returns a long (that means a long integer) number. This function can be used in two ways. The first is calling it with the max range of probable number that you want the function to produce.
For example if you want the  function to return a single digit integer you will call it this way:

random(10);


Notice that just like in C-language code the number start from 0, so 0-9 are 10 numbers.
The second call is by providing a range in which you want the number to be in.

random(0,10);

This will still return a number between 0 and 9.