Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Random instruction problem


GAG Oct 4, 2017 02:28 PM

There is a need to use Random instruction in CRBasic program, the following is my the program:

'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   'Randomdata
    Counter +=1
    RealTime( raTime())
    RandomSeed_Counter += raTime(6)
    Randomize( RandomSeed_Counter)
      
    If True = MCLR_Power  Then
      MCLR = INT(((MCLR_Max-MCLR_Min) * RND + MCLR_Min)*1000 )
      MCLR = MCLR / 1000
      If MCLR_Max = MCLR_Min Then
        MCLR = 0
      EndIf
      If raTime(5) MOD MCLR_Period = 0 Then
        MCLR_DataTableFlag = False
        CallTable MCLR_DataTable
      Else
        MCLR_DataTableFlag = True
      EndIf
    EndIf

'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

If just using the program in your example program, the data is too regular. So a counter is added to make the data more random. However, it seems still have some pattern in data. Please see the picture below.

Sorry I cannot insert the picture in this web.

Can you give me some help to solve this? 

Guo An'gang


JDavis Oct 4, 2017 03:13 PM

Seeding off of seconds does not work well. You should just seed at program compile before the scan with a number that is unpredictable. The time of the datalogger works sufficiently well most of the time.

 

BeginProg

'Initialize random number generator using seconds since 1990 + microseconds in datalogger clock
Randomize(Status.Timestamp(1,1) + Status.Timestamp(7,1))

Scan (1,Sec,0,0)

 

The pseudo random sequence does repeat, but it takes something like 2 billion calls to repeat itself.

If you want a truly random number, you need to use electrical noise. That is what the random generator unit in modern Intel processors use. This process takes time. Put a high value resistor between a SE channel and ground. The last digit of resolution on that channel is noise.

 This example program generates a truly random 128 bit number used as an encryption key. You can probably get by with just generating a 32 bit number.

'CR6 Series Datalogger
Public Random(4) As Long
Public BinaryString As String * 132
Dim i As Long, k As Long
Dim rawVolts(128)
Dim tempLong As Long

'Main Program
BeginProg
  Scan (1,Sec,0,0)
    VoltSe (rawVolts(),128,mV2_5C,-3,0,595,250,1.0,0)

    Erase(Random)
     '  Fill Longs with random bits
    For k = 1 To 4
      For i = 0 To 30
        MoveBytes (tempLong,3,rawVolts(1 + ((k-1)*32) + i ),3,1)
        Random(k) +=    (tempLong MOD 2) << i
      Next i
      MoveBytes (tempLong,3,rawVolts(k*32)  ,3,1)
      Random(k) *= (1 + (-2 * (tempLong MOD 2))) 'Sign bit
    Next k

    BinaryString = FormatLong (Random(1),"%032b") & FormatLong (Random(2),"%032b") & FormatLong (Random(3),"%032b") & FormatLong (Random(4),"%032b")
  NextScan
EndProg

 


rlwoell Oct 4, 2017 03:19 PM

The best seed numbers to use are large, odd and prime if possible.  One of my favorites in simulation classes was 49,999.  Even though it does repeat like all pseudo random generators to, the period is very long.


GAG Oct 9, 2017 02:22 AM

Thank you very much.

We have a National Day, sorry for a late reply.

I will test the method you have listed.

Thanks again.

Log in or register to post/reply in the forum.