PSEUDO-Random generator in STL/SCL

duckie112

Member
Join Date
Mar 2012
Location
In a cardboard box
Posts
46
Hi, for an assignment on school I need to make a PSEUDO-random generator coded in STL/SCL wich needs to make random numbers from 50-999 and from 5-30 minutes. I have searched the forums and came across some but couldn't figure out how they worked and if they were for Siemens S7. Also the random numbers need to be stored in 2 MW markers and the function needs to be called between 1-5 minutes.
 

I tried that when I was searching the forums a bit more only found that 1 to be actual useful a bit. Heres the problem I have with it: Whenever I write that code in my FC2 block it says it cant copy the block to the PLC anymore. And whenever I delete that code the block can be copied again. And whats exactly inside DB1?
I understand the first part a bit but the 2nd part I'm having problems with. First if I change L 100 to
L 1000 and MOD that will it be range 1-999 then? Second can I store the random number in like say MW20 since the last part from SLD 4 I don't quite understand. (I'm practically new to SCL/STL and learning it for the last 3 weeks.)
EDIT: Never mind :p got it working but it seems it makes random numbers higher then 100. The values its generating is good enough for my random minutes but not good enough for the random 1-999. Also is it possible to generate the numbers every minute? Since its generating it very fast now and they need to stay like a minute the same.
 
Last edited:
The 0-999 part with random parts is solved with /D 1000 from the result so it stick with a Rounded value between 0-999 (sometimes above 1k but thats almost never) also I was able to call the block every 15 minutes now (forgot I had a looped mechanism wich goes on for 14 then off for 2 seconds and go on for 14 again etc.
 
The second part counts the occurances of the numbers between 0-99 and stores the counts in DB1. I imported the DB into xcel to draw the charts.
 
See this
http://www.codeproject.com/KB/recipes/SimpleRNG.aspx


If I had to do this over again I would probably make it a function block with m_w and m_z declared as VARs. I would still need some way of randomizing the initial value of m_w and m_z.


Code:
(*
	RNG calculates random numbers from 0 to 1.
	RNG requires two in_out variables to maintain state.
*)

FUNCTION RNG : REAL
	VAR_IN_OUT
		m_z : DWORD;
		m_w : DWORD;
	END_VAR
	VAR
		u: DINT;
	 END_VAR
	m_z:=DINT_TO_DWORD(36969*DWORD_TO_DINT(m_z AND 16#FFFF)+DWORD_TO_DINT(SHR(m_z,16)));
	m_w:=DINT_TO_DWORD(18000*DWORD_TO_DINT(m_w AND 16#FFFF)+DWORD_TO_DINT(SHR(m_w,16)));
	u :=  DWORD_TO_DINT( SHL(m_z,16))+DWORD_TO_DINT(m_w);
	RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10+0.5;
END_FUNCTION
 
See this
http://www.codeproject.com/KB/recipes/SimpleRNG.aspx


If I had to do this over again I would probably make it a function block with m_w and m_z declared as VARs. I would still need some way of randomizing the initial value of m_w and m_z.


Code:
(*
	RNG calculates random numbers from 0 to 1.
	RNG requires two in_out variables to maintain state.
*)

FUNCTION RNG : REAL
	VAR_IN_OUT
		m_z : DWORD;
		m_w : DWORD;
	END_VAR
	VAR
		u: DINT;
	 END_VAR
	m_z:=DINT_TO_DWORD(36969*DWORD_TO_DINT(m_z AND 16#FFFF)+DWORD_TO_DINT(SHR(m_z,16)));
	m_w:=DINT_TO_DWORD(18000*DWORD_TO_DINT(m_w AND 16#FFFF)+DWORD_TO_DINT(SHR(m_w,16)));
	u :=  DWORD_TO_DINT( SHL(m_z,16))+DWORD_TO_DINT(m_w);
	RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10+0.5;
END_FUNCTION

One question the last line
Code:
RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10+0.5;
is different from the original at http://www.codeproject.com/KB/recipes/SimpleRNG.aspx is this because the PLCs inferior resoulution??

If written as the original:
Code:
RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10;
 
One question the last line
Code:
RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10+0.5;
is different from the original at http://www.codeproject.com/KB/recipes/SimpleRNG.aspx is this because the PLCs inferior resoulution??

If written as the original:
Code:
RNG :=DINT_TO_REAL(u+1)*2.328306435454494E-10;
With out the 0.5 the routine returns -.4999999 to 0.49999999. The 0.5 adjust the random number to be between 0.0 and 1, that's all.
 

Similar Topics

I have an application where I am converting a binary 2 digit number (00-99) to BCD to display on a 7 segment display. Unfortunately I don't have...
Replies
12
Views
4,270
I wish to develop 2 "standard" plc programs and matching HMI applications. One program is intended to ultimately run on a CompactLogix PLC and...
Replies
8
Views
2,984
Hello Everyone, I need a simple solution for my customer who doesn't want ControlNet redundant (CNBR) modules, he want only CNB modules with 2...
Replies
0
Views
2,254
Looking for some advice for backing up the programs on a bunch of old random PLCs. All I'm really concerned about is getting the program out of...
Replies
43
Views
4,748
We have several Powerflex 4M (22F-D013N114) VFD’s controlled by compactlogix PLC’s. Control is simple, a run and direction signal with the speed...
Replies
6
Views
1,546
Back
Top Bottom