News:

  • August 25, 2026, 09:15:59 AM

Login with username, password and session length

Author Topic: Random Integer Range  (Read 9770 times)

Evilbeard

  • Hero Member
  • *****
  • Posts: 160
Random Integer Range
« on: April 04, 2017, 10:35:39 AM »
So, how much hassle would it be to create a box/function this is RandInt(XX) where XX is the number of digits? It'd be nice for simulation. Just spitballing.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Random Integer Range
« Reply #1 on: April 04, 2017, 11:35:30 AM »
Done.  I wrote a subroutine (Do-more Technology version 2.0 supports subroutines w/parameters) called RandIntRange.

It takes 2 input parameters, the Low and High Integer range values, and 1 output parameter, the random result of uniform distribution between those 2 integers.

Attached is a screen shot of a program that simulates rolling a die (i.e. RandIntRange 1 to 6) once a second.  I put the result into D0, so my trend view is of D0.

Within the subroutine, D100 is the "lo" input, D101 is the "high" input, and D102 is the "result" output.  The actual nicknames are _RIR_In_LO, _RIR_In_HI, and _RIR_Out_RandVal, respectively.

The MATH formula in the subroutine auto corrects in case you get the Lo and Hi backwards (using ABS and MIN MATH functions):
MATH D102 "((ABS(D101 - D100) + 1) * RANDREAL()) + MIN(D100, D101)"

I've attached the import project RandIntRange.txt you can import into Designer 2.0 and download to your simulator using File->Import->Project

You could change the CALL RandIntRange in $Main to take 100000 and 200000 for the 2 input parameters (instead of 1 and 6 as Low and High) to get random integers between those two numbers put into D0.

You can have as many CALL RandIntRange instructions throughout your ladder logic to simulate rolling 2 dice, simulate a batch result between 90 and 100, et. al.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Random Integer Range
« Reply #2 on: April 04, 2017, 12:06:57 PM »
I tweaked the program to do a random number between 500,000 and 1,000,000 every 50 milliseconds, then did a 1.0 second FILTER on D0 and the FILTER Mini Trend view shows the filter result (R0) hovering around 750,000 (see attached screen shot).

FILTER is sort of like doing a rolling average statistically, so hovering around 750,000 is what we would expect to see!

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3626
  • Darth Ladder
Re: Random Integer Range
« Reply #3 on: April 04, 2017, 01:33:04 PM »
What does the '+1' do?  Adjust for truncation when demoting to DINT at the end?
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Random Integer Range
« Reply #4 on: April 04, 2017, 01:45:20 PM »
What does the '+1' do?  Adjust for truncation when demoting to DINT at the end?

Yes.  Math modelers know that a uniform range of [0.0, 1.0) (i.e. include 0.0 but do NOT include 1.0) provides the precise mechanism for creating a random distribution of whole numbers (like rolling a die).  So the +1 will cause a uniform range starting at 1.0 up to 6.99999 (but not 7.0).

RandReal() does exactly that, hence the need for the "+ 1".