JimEb (Customer) asked a question.

Productivity 1000 - random number?

Got and application where I need some random numbers from 1-100 generated. Didn't find a canned instruction to do that so I did accomplish something using a cobbled amalgamation of the ramp generator, timer and internal clock. Just wondered if there was a canned feature for this.

 

Or...since this application is also counting pulses off a spinning motor I figure if I could just figure out how to capture just the last two digits off the ever growing pulse count number then I've got a random number as good as any.


Garry likes this.
  • ADC Community_02 (Automationdirect.com)

    We just released version 4.2.0.39 that has several new improvements to the MATH instruction including "random" that generates a random floating point number between 0 and 1 with 15-bit resolution. 

    Selected as Best
  • z28z34man (Customer)

    A P1-AM100 would have a random number instruction but i believe you are correct in that the productivity suite dose not have a built in random number instruction.

  • kewakl (Customer)

    Do you need anything approaching true randomness? How random do you need?

     

    • JimEb (Customer)

      Doesn't need to be truly random. This is on my homebrew treadmill application. All this random number does is display a 'motivational quote' off a message database on the HMI to break up the monotony of running on a treadmill. Having more control over the range of random numbers would give me more control on how sporadic the messages pop up.

      • kewakl (Customer)

        You could put the project (below) into the software simulator to see if it appears random enough. I think that you might notice a repetition after a while, but then, you could change the mask to 'reshuffle' the series of numbers.

        1. Mask
        2.  
        3. 0xB400 0xB402
        4.  
        5. Returns these series of numbers
        6. 22128 22128
        7.  
        8. 28984 28985
        9.  
        10. 14492 14492
        11.  
        12. 7246 17999
        13.  
        14. 3623 8999
        15.  
        16. 1811 19346
        17.  
        18. 22921 32712
        19.  
        20. 30404 16356

         

        Expand Post
  • kewakl (Customer)

    I posted this (Galois LFSR) project on the old forum (now lost). You could take the trailing 2 digits as you mentioned earlier, but without the hardware requirements -- meaning that the values could be acquired without machine motion.

     

    You will need to supply the 'lsfr', _MASK_ and 'startstate.'

    These values are already in the dataview.

    lsfr ( yes, a typo) hex value 0xACE1

    _MASK_ hex value 0xB400

    startstate hex value 0xACE1

     

    My interpretation of the wiki Toc Heading

    - https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Galois_LFSRs

    Expand Post
  • JimEb (Customer)

    Update in case other people with the same question land on this thread.

    Using the rapidly and ever increasing pulse count off a prox switch I used the modulus math function to chop it down to 2 digits for a random number.

    (PULSES % 100) will give you just the last two digits. ie: 154685485487 % 100 = 87. If you wanted a single digit you could do: 154685485487 % 10 = 7, etc.

     

    I execute that rung with a 1-shot contact off the internal 1 minute bit and every minute yields a new random number (00-99).

    Expand Post
    • PouchesInc (Customer)

      A good solution to your problem.

       

      I was going to suggest a modulo type of math but then remembered that I had tried that once before in productivity with a "mod()" expression in the math block and Productivity didn't understand it so I thought you couldn't do those types of operations in Productivity. I'm glad you figured it out with the "% 100" expression.

      • kewakl (Customer)

        The MATH instruction's '%' symbol is the MODULO operator.

        Am I missing something?

      • PouchesInc (Customer)

        I'm not really a math person, especially not in computer coding, so I didn't know the % sign in the math block was how you write that stuff out here. The only examples I had seen before called out "mod" and were written as "n mod x" or "mod(n,x)"

      • kewakl (Customer)

        Understood.

         

        In PAC math 17 % 2 is equivalent to 17 MOD 2 and i s equivalent to excel =mod(17,2)

        In OPs case NUMERICTAG % 100 will return any (division result) remainder up to 2 digits.

         

        12344 % 100 should return 44

        32 % 100 should return 32

         

        Hope this helps in the future.

        Expand Post
10 of 15