• PicŪ Basic


  • A true random number generator

    The best any microcontroller can create is a pseudo random number based upon a seed value. For a true random value, an external influence is required. In our case, we'll use an open ADC channel to create a random bit, with which, we'll create a random byte or random word.

    The random number generator is in the form of an include file named "Real_Random.inc", which is placed at the top of the main program code.

    The include file contains several macros, of which 4 are of interest to us. These are:

    Random_GetBit

    Syntax
    Variable = Random_GetBit()

    Returns a random bit. i.e. 0 or 1

    Example
    Code:
    ' Create a random bit and display it on the serial terminal
    
      Include "Amicus18.inc"       ' Configure the compiler for a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
    
      $define Random_ADC_Channel 0 ' Use AN0 as the ADC channel to use for the random bit generator
      Include "Real_Random.inc"    ' Load the random macros into the program
    
      Dim MyBit as Bit             ' Create a bit variable to hold the random value
    
      While                        ' Create and endless loop
        MyBit = Random_GetBit()    ' Get a random value into MyBit
        Hrsout Dec MyBit, 13       ' Display the value on the serial terminal    
        DelayMs 400                ' Delay so we can see what's happening
      Wend
    Random_GetByte

    Syntax
    Variable = Random_GetByte()

    Returns a random byte between 0 to 255

    Example
    Code:
    ' Create a random byte and display it on the serial terminal
    
      Include "Amicus18.inc"       ' Configure the compiler for a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
    
      $define Random_ADC_Channel 0 ' Use AN0 as the ADC channel to use for the random bit generator
      Include "Real_Random.inc"    ' Load the random macros into the program
      
      Dim MyByte as Byte           ' Create a byte variable to hold the random value
      
      While                        ' Create and endless loop
        MyByte = Random_GetByte()  ' Get a random value into MyByte
        Hrsout Dec MyByte, 13      ' Display the value on the serial terminal    
        DelayMs 400                ' Delay so we can see what's happening
      Wend
    Random_GetWord

    Syntax
    Variable = Random_GetWord()

    Returns a random word between 0 to 65535

    Example
    Code:
     ' Create a random word and display it on the serial terminal
    
      Include "Amicus18.inc"       ' Configure the compiler for a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
    
      $define Random_ADC_Channel 0 ' Use AN0 as the ADC channel to use for the random bit generator
      Include "Real_Random.inc"    ' Load the random macros into the program
      
      Dim MyWord as Word           ' Create a word variable to hold the random value
      
      While                        ' Create and endless loop
        MyWord = Random_GetWord()  ' Get a random value into MyWord
        Hrsout Dec MyWord, 13      ' Display the value on the serial terminal    
        DelayMs 400                ' Delay so we can see what's happening
      Wend
    Random_Get

    Syntax
    Random_Get(pMaxValue, pResult)

    Loads parameter pResult with a value between 0 and (pMaxValue - 1)
    pMaxValue has a maximum value of 32767.

    Example
    Code:
     ' Create a random value between 0 and 9 and display it on the serial terminal
    
        Include "Amicus18.inc"       ' Configure the compiler for a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
    
      $define Random_ADC_Channel 0 ' Use AN0 as the ADC channel to use for the random bit generator
      Include "Real_Random.inc"    ' Load the random macros into the program
      
      Dim MyByte as Byte           ' Create a byte variable to hold the random value
      
      While                        ' Create and endless loop
        Random_Get(10, MyByte)     ' Get a random value between 0 and 9 into MyByte
        Hrsout Dec MyByte, 13      ' Display the value on the serial terminal    
        DelayMs 400                ' Delay so we can see what's happening
      Wend
    Notes.
    The ADC channel to use for the random bit generator can be chosen with a $define:

    $define Random_ADC_Channel 0 or 1 or 2 or 3 or 4

    A value of 0 will use AN0, 1 will use AN1 etc...

    Always add the $define before the Real_Random.inc file is included into the program, so it can be seen by the routines.

    If the $define is not used in the main program, ADC channel AN0 will be used.

    It is important to make sure that the ADC channel chosen is not connected to anything, as it must be left floating.

    The Real_Random.inc file can be downloaded from here: Real_Random

    The zip file also contains three demonstration programs:

    Real_Lottery_Predictor.bas
    Real_Random_Demo.bas
    Real_Random_Dice.bas

  • Recent Activity

    joesaliba-353

    Pulse counter memory problem

    Thread Starter: amod

    Please tell me my faults.My pulse counter memory does not store.When i switch on device after 5 second ,memory exist by after 30 seconds it is washed...

    joesaliba Yesterday, 18:33 Go to last post