📄 page467.html
字号:
<HTML><HEAD><TITLE>Implementation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6547" HREF="page468.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6545" HREF="page465.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6541" HREF="page466.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6549" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0014512000000000000000">Implementation</A></H3><P>We now describe the implementation of a random number generatorbased on Equation <A HREF="page466.html#eqnalgsschrage"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Program <A HREF="page467.html#prograndomNumberGeneratora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>defines the <tt>RandomNumberGenerator</tt> class.<P><P><A NAME="33981"> </A><A NAME="prograndomNumberGeneratora"> </A> <IMG WIDTH=575 HEIGHT=661 ALIGN=BOTTOM ALT="program33808" SRC="img1921.gif" ><BR><STRONG>Program:</STRONG> <tt>RandomNumberGenerator</tt> class <tt>__init__</tt> method, and <tt>__seed__</tt> and <tt>next</tt> properties.<BR><P><P>The <tt>seed</tt> propertyis implemented using the <tt>getSeed</tt> and <tt>setSeed</tt> methods.The <tt>setSeed</tt> method is usedto specify the initial seed, <IMG WIDTH=20 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline68501" SRC="img1887.gif" >.The seed must fall between 0 and <I>m</I>-1.The <tt>getSeed</tt> method returns the current seed value.<P>The <tt>next</tt> propertyis implemented using the <tt>getNext</tt> method.the <tt>getNext</tt> methodgenerates the elements of the random sequence.Each subsequent call to <tt>getNext</tt>returns the next element of the sequence.The implementation follows directly from Equation <A HREF="page466.html#eqnalgsschrage"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Notice that the return value is normalized.Therefore, the values computed by the <tt>getNext</tt> accessor are uniformlydistributed on the interval (0,1).<P>The assignment statement on line 34of Program <A HREF="page467.html#prograndomNumberGeneratora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is interesting.It creates an <em>instance</em> of the <tt>RandomNumberGenerator</tt> classand then names that instance <tt>RandomNumberGenerator</tt>.This has the effect of hiding the class definition andthereby preventing the creation of any additional instances of the class.Because there can only be one instanceof the <tt>RandomNumberGenerator</tt> class,this is an example of the <em>singleton</em><A NAME=33847> </A> design pattern.<P><HR><A NAME="tex2html6547" HREF="page468.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6545" HREF="page465.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6541" HREF="page466.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6549" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -