• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
Previous Next

Generate Random Numbers on pcDuino

Posted by: , April 18, 2014

[vc_row][vc_column width=”1/1″][vc_column_text]In this post, we will look at how to generate random numbers on pcDuino[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Random Numbers for pcDuino” tab_id=”1397790749-1-94″][vc_column_text]pcDuino is compatible with Arduino interface and function, so pcDuino can directly use the Arduino random function  Random ()  to create a random number. As pcDuino supports many other programming languages, we can also use the C language random number function rand () to realize it.

Below we will look at two different ways to generate random numbers on pcDuiono.[/vc_column_text][/vc_tab][vc_tab title=”Random function for Arduino” tab_id=”1397790749-2-67″][vc_column_text]The format of Arduino random function is as follows

int maxNum = 10;
int minNum = 1;
long randomNum = random (maxNum);
long randomNum = random (minNum, maxNum);

In fact the Arduino random function is based on a certain software algorithm. It does not use any random hardware functions.  When the random factor(we can also call this seed) is not changing, the generated random number will not change. We can write a small program to verify

#include "core.h"
int randomNumber = 0;
int loopControl = 0;
int randomPin = A5; 
void setup()
{ }
void loop()
{
 //int num = analogRead(A5); 
//printf ("num = %dn", num); 
//randomSeed (num);

 for (loopControl = 1; loopControl <= 100; loopControl++)
{
 randomNumber = random (1, 101);
 printf ("%4d", randomNumber); 
if (!(loopControl % 10 ))
  { printf ("n"); }
 }
 exit(0);
 }

Copy the above program to  the sample directory and rename random_arduino. C
Modify the Makefile. Then execute the executable random_arduino, results are as follows
random_arduino_error

We run 3 times and  find each time the 100 printed  Numbers are the same.
So we have to set a random factor through the randomSeed () function. Arduino generally adopt read A0 – A5 values as random factor, because if there is no external signal incoming, the sampling values of ADC on the arduino will change randomly, unpredictable.
So pcDuino principle can also be used , such as:

randomSeed (analogRead (5))
To set the random factor. But in fact pcDuino and arduino are different.
PcDuino A0 – A5 mouth are the default when there is no external signal connection, would read the maximum all the time.
So we connect a photo resistance between A5 and GND then open the comments part of the code above.
The result as the shown
random_arduino_right

Num read out the value of the analogRead read out. .Followed by a new 100 random numbers.

We also performed three times. The generated random number is changed.

[/vc_column_text][/vc_tab][vc_tab title=”Random functions of C language” tab_id=”1397791264062-2-3″][vc_column_text]Then we use the srand function and rand function of C language to do the experiment.

The function of the srand function is similar to the randomSeed, they all generate a random factors,  here we use roll forward time as random factors, because time always change. Program  is as follows

#include "core.h"

int loopControl = 0;
int randomNum = 0;

void setup(){}

void loop(){
    for (loopControl = 1; loopControl &lt;= 100; loopControl++){
        srand(time(0));
        randomNum = rand()%100 + 1;
        printf ("%4d", randomNum);
        delay (1000);
        if (!(loopControl %  10)){
            printf ("n");
        }
    }
    exit(0);
}

Test result:
random_pcDuino_right[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: Arduino-ish Program

Share!
Tweet

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors