S7 Random Number Generator

RRobbins

Lifetime Supporting Member
Join Date
Dec 2002
Location
Richmond, VA
Posts
405
Does anyone have a GOOD random number generator for a Siemens S7-300? I am trying to generate a number between 0 and 313.
 
Don't know about Siemens instruction set these days. Thankfully, have not had to use them for years.

Use a differentiated increment instruction. Use the scan flag or a 0.1 second internal clock pulse as the input. You couls even use both. When the number reaches 313, move "0" into the increment channel. Let it run all the time.

When you require to use the random number, use a differentiated move to move the number into the channel where the number is required.

If you wish to increase randomness, use 2 channels, 0-9 in the first and 0-13 in the second. The same applies to limits etc but you would probably have to use a move digit command to read the number into the required channel.

It works and is simple.
 
F = F * PI ; REAL
D = FLOOR(F) ; DINT (OR ROUND)
DR = DI_R(D) ; REAL
F = F - DR ; REAL

E = F*313.0 ; REAL
G = FLOOR(E) ; DINT
MOVE G TO X ; INT
 
Try this:

CALL "TIME_TCK"
RET_VAL:=#tTime

L #tTime
L 32
MOD
TAK
RLD
DTR
ABS
L 2.147484e+009
/R

It returns pseudorandom number 0.0 .. 1.0 in ACCU1. TIME_TCK is SFC64. If it's any good ? - I don't know.
 
random numbers

The proposals yet made will not provide you with a GOOD random number generator. GOOD in the sense that they will not generate predictable sequences.
See:
http://www.library.cornell.edu/nr/bookcpdf.html

There you can get the chapters of a book "Numerical recipes" that provides algorithms for and discusses the maths behind random numbers.
These algorithms may eat up to much cycle time from your PLC. If so, you could connect some noisy signal to an anlog input. Use the lower bits. If you have e.g. only the lowest two bits changing randomly, you can shift bits from 5 or 9 conversions into a shift register. This will result in a 10 or 18 bit string. Take 9 bits from it, copy to a word anc calculate modulo 313.
 
Last edited:
Seppo, once I did a compare F to 0.0 and stuck a bogus number in to it, to get it started, it worked like a champ. Thanks.
 
The proposals yet made will not provide you with a GOOD random number generator. GOOD in the sense that they will not generate predictable sequences.

I thought a good random number generator was not supposed to generate predictable sequences. The number would not be random if it was predictable.
 
The proposals yet made will not provide you with a GOOD random number generator.
GOOD in the sense that they will not generate predictable sequences.

I wanted to say: a good random number generator will not generate predictable sequences.
 
I wanted to say: a good random number generator will not generate predictable sequences.
If that is what you mean, then there is no programmed random generator that will do. Being programmed means it's based on a mathematical formula, and this in turn means it will always be predictable. All you need to do to predict the next value is calculate it. This is why this kind of random generators is always called PSEUDO random generator. The idea behind them is: if the sequence is long enough, then it looks as if it's random. Most people however forget to mention the term pseudo.

Even a random number generated from the noise in a component could be calculated, provided you would know ALL the parameters, something that isn't accomplished yet. The only thing really random in the universe is quantum physics. And that is way above my head (and that of most of us here).

Kind regards,
 
Just to re-ignite an old thread!

I'm looking to make use of a random function for testing purposes.

When I press a button, I want to make a bit active for 1 second. This bit will be a random DB bit from DBX0.0 to DBX62.4 (500 bits)

I've tried using the suggestions above but not sure how to limit the random number to between 0 and 500.

And it doesn't have to be truly random, I just want it to select a different bit each time I press the button.
 
@L D[AR2,P#0.0] @Peter Nachtwey

I think I well over-thought this. A simple counter up to 500 (then reset) will do for my needs, when I press the button it will save a number somewhere between 0 and 500.

But I will reserve the random number ideas for future use!

Thanks guys!
 

Similar Topics

Hey folks, I was just wondering what thoughts were about a completely 100% random number generator. I looked on The Google but didn't really open...
Replies
54
Views
16,436
Hello, Could somebody please help me out with coding a 'Random number generator' in vijeo citect. For example if i create an analog Local...
Replies
3
Views
3,726
Hi,Does anybody know if there are any routines in unity pro that produce random numbers like the routine rand() in C ?
Replies
1
Views
4,021
Ive got a client that needs random numbers generated for a security system. Just want to know if any 1 has some ladder or ST software or scripts...
Replies
11
Views
8,804
Can anyone help me create a few ladder logic lines to randomly generate intergers btw 0-100. I have tryed loading the scan timer value, then ANDD...
Replies
16
Views
13,278
Back
Top Bottom