Author Topic: Generacting a random event or number  (Read 1362 times)

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Generacting a random event or number
« on: March 14, 2016, 02:39:55 AM »
Hi all.

I have a need to change a function randomly and am trying to work out a way of doing this. As an example, I need to change the direction of a motor running. SO it runs one direction for a while then stops and changes rotation. Any suggestions?
« Last Edit: March 14, 2016, 02:40:19 AM by secs »

garysdickinson

  • Expert Member
  • Posts: 458
  • Old PLC Coder
    • View Profile
Re:Generacting a random event or number
« Reply #1 on: March 14, 2016, 04:42:07 AM »
If you are using one of the Fx series PLCs, TBASIC has RND() function to return a random number as a float.

If you are using an FMD or Nano-10 PLC, TBASIC doesn't have a random number function.

There are many published algorithms for generating random numbers that could be implemented in TBASIC.

I use a "pseudo random number generator" that is based on CRC algorithms.  These generators will return an integer between 0 and 65,535.  The sequence of values will repeat after 65,536 calls to the functions.  It isn't 100% random, just "Pseudo random".

I will give you 2 different CRC based algorithms.  Each of these algorithms, use the value in "C" and will replace "C" with a pseudo random value.  I have used them both for purposes similar to what you described.

  • This version implements a CRC using shift and XOR

' This generator is based on a common 16 bit CRC algorithm.
'
if C = 0
   C = &Hffff    ' Reseed CRC if output goes to 0
endif

IF    C & &h8000
    LSHIFT C,1
    C = C ^ &H1021
ELSE
   LSHIFT C,1
ENDIF

C = C & &Hffff    ' Limit output to 16 bit integer
[/font][/color]

  • This is another variation that uses TBASIC CRC16() function.

' Random number generator using the TBASIC CRC16 function
'
DM[1] = C & &hff : DM[2] = (C / 256) & &hff   ' Copy the seed value from C to DM
C = CRC16(DM[1],2)                     ' Compute next pseudo random number in C
[/font][/color]
[/list]

Gary D*ickinson
« Last Edit: March 14, 2016, 05:03:24 AM by garysdickinson »

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Re:Generacting a random event or number
« Reply #2 on: March 14, 2016, 09:50:52 PM »
Many thanks Garry. I will give it a try.

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Re:Generacting a random event or number
« Reply #3 on: March 15, 2016, 07:26:41 AM »
Yep works well.

I am actually working on a mechanical bull and the customer is excited. Thanks to you Garry :-)

garysdickinson

  • Expert Member
  • Posts: 458
  • Old PLC Coder
    • View Profile
Re:Generacting a random event or number
« Reply #4 on: March 15, 2016, 04:47:44 PM »
Always happy to help.

If you use the CRC16() version of the code, as it is simpler, I'd suggest that you test the code carefully. The thing to look out for is issues with the output value getting stuck at 0 or the data pattern repeating at short intervals.

I have used this type of random number generators On several occasions.  If you limit the random values to a range of 1..9999 you can program the SV of counters or timers directly.  

Most of the time I don't have 24/7 access to the client's system that the PLC is intended to control. I set up a second PLC to simulate the key behaviors of the client's system.  I use the random number stuff to mix up the simulated system's behavior.  This way I can easily verify that the "real" PLC code can cope with lots of odd system behavior and can handle error conditions that might be either impossible or dangerous to create with the customer's actual system.

Gary d
« Last Edit: March 15, 2016, 04:55:40 PM by garysdickinson »