$Random: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (misc cleanup)
 
(45 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:$Random}}
{{DISPLAYTITLE:$Random}}
<span class="pageSubtitle"><section begin="desc" />Get next random number<section end="desc" /></span>
<span class="pageSubtitle">Get next random number</span>


<p class="warning">Most Sirius $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $Random function is [[to be entered]].</p>
<p class="warn"><b>Note: </b>Many $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $Random function is the <var>[[RandomNumberGenerator class|RandomNumberGenerator]]</var> class.</p>


This function returns either an unpredictable random number, or the next number in a series of random numbers.  
This function returns either an unpredictable random number, or the next number in a series of random numbers.  


$Random accepts three optional arguments and returns an integer number.
==Syntax==
<p class="syntax"><span class="term">%result</span> = <span class="literal">$Random</span>([<span class="term">minimum</span>], [<span class="term">maximum</span>], [<span class="term">%seed</span>])
</p>


The first argument specifies the smallest value that will be returned; the minimum value of this argument is the same as its default: -1,000,000,000.  
<table>
<tr><th>%result</th>
<td>A numeric variable to receive an integer greater than or equal to <var class="term">minimum</var> and less than or equal to <var class="term">maximum</var>.</td></tr>


The second argument specifies the greatest value that will be returned; the maximum value of this argument is the same as its default: 1,000,000,000. This argument must be greater than the smallest value that will be returned.
<tr><th>minimum</th>
<td>The smallest value that will be returned; the minimum value of this argument is the same as its default: -1,000,000,000. </td></tr>


The third argument is a %variable or IMAGE item that was set to a random number seed using $Random_Seed. This argument must be a string of length at least 144. If this argument is omitted, the number returned is unpredictable; if it is present, the seed determines a specific series of random numbers, and each call to $Random returns the next number in that series.
<tr><th>maximum</th>
==Syntax==
<td>The greatest value that will be returned; the maximum value of this argument is the same as its default: 1,000,000,000. This argument must be greater than the smallest value that will be returned. </td></tr>
<p class="syntax"><section begin="syntax" /> %RESULT = $Random(minimum, maximum, %SEED)
<section end="syntax" /></p>
<p class="caption">$Random Function
</p>
<p class="caption">%RESULT contains a random number greater than or equal to '''minimum''' and less than or equal to '''maximum'''.</p>


<tr><th>%seed</th>
<td>A %variable or IMAGE item that was set to a random number seed using <var>[[$Random_Seed]]</var>. This argument must be a string of length at least 144.
<p>
If this argument is omitted, the number returned is unpredictable; if it is present, the seed determines a specific series of random numbers, and each call to <var>$Random</var> returns the next number in that series. </p></td></tr>
</table>


If any argument is invalid, the request is cancelled.  
If any argument is invalid, the request is cancelled.  


In the following example
==Examples==
<p class="code"> %I = 5
<ol>
REPEAT WHILE %I GT 0
<li>In the following example an unpredictable hand of 5 playing cards is "dealt".
%N = $Random(1, 52)
<p class="code">%I = 5
IF %CARD(%N) NE '' THEN
REPEAT WHILE %I GT 0
PRINT %CARD(%N)
  %N = $Random(1, 52)
%I = %I - 1
  IF %CARD(%N) NE &apos;&apos; THEN
%CARD(%N) = ''
    PRINT %CARD(%N)
END IF
    %I = %I - 1
END REPEAT
    %CARD(%N) = &apos;&apos;
  END IF
END REPEAT
</p>
</p>
an unpredictable hand of 5 playing cards is "dealt".


In the following example
<li>In the following example a hand of 5 playing cards is "dealt", and the "player" can either ask for the particular hand to play, or can just hit enter, and get an unpredictable hand.
<p class="code"> %HAND STRING LEN 144
 
REPEAT FOREVER
<p class="code">%HAND STRING LEN 144
%X = $READ('Pick a 4-digit hand number')
REPEAT FOREVER
IF %X EQ 0 THEN
  %X = $READ('Pick a 4-digit hand number')
%X = $Random(1, 9999)
  IF %X EQ 0 THEN
END IF
      %X = $Random(1, 9999)
IF %X GT 0 AND %X LT 10000 THEN
  END IF
LOOP END
  IF %X GT 0 AND %X LT 10000 THEN
END IF
      LOOP END
PRINT 'I said a 4 digit number!'
  END IF
END REPEAT
  PRINT 'I said a 4 digit number!'
PRINT 'You are playing hand number' AND %X
END REPEAT
%X = $Random_Seed(%HAND, %X)
PRINT 'You are playing hand number' AND %X
%I = 5
%X = $Random_Seed(%HAND, %X)
REPEAT WHILE %I GT 0
%I = 5
%N = $Random(1, 52, %HAND)
REPEAT WHILE %I GT 0
IF %CARD(%N) NE '' THEN
  %N = $Random(1, 52, %HAND)
PRINT %CARD(%N)
  IF %CARD(%N) NE &apos;&apos; THEN
%I = %I - 1
      PRINT %CARD(%N)
%CARD(%N) = ''
      %I = %I - 1
END IF
      %CARD(%N) = &apos;&apos;
END REPEAT
  END IF
END REPEAT
</p>
</p>
a hand of 5 playing cards is "dealt", and the "player" can either ask for the particular hand to play, or can just hit enter, and get an unpredictable hand.
</ol>
 
==Products authorizing {{PAGENAMEE}}==
<ul class="smallAndTightList">
<ul class="smallAndTightList">
<li>[[Sirius functions]]</li>
<li>[[List of $functions|Sirius functions]]</li>
<li>[[Fast/Unload User Language Interface]]</li>
<li>[[Fast/Unload User Language Interface]]</li>
<li>[[Janus Open Client]]</li>
<li>[[Media:JoclrNew.pdf|Janus Open Client]]</li>
<li>[[Janus Open Server]]</li>
<li>[[Media:JosrvrNew.pdf|Janus Open Server]]</li>
<li>[[Janus Sockets]]</li>
<li>[[Janus Sockets]]</li>
<li>[[Janus Web Server]]</li>
<li>[[Janus Web Server]]</li>
<li>[[Japanese functions]]</li>
<li>Japanese functions</li>
<li>[[Sir2000 Field Migration Facility]]</li>
<li>[[Media:SirfieldNew.pdf|Sir2000 Field Migration Facility]]</li>


</ul>
</ul>
<p class="caption">Products authorizing $Random
</p>


[[Category:$Functions|$Random]]
[[Category:$Functions|$Random]]

Latest revision as of 18:40, 21 April 2016

Get next random number

Note: Many $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $Random function is the RandomNumberGenerator class.

This function returns either an unpredictable random number, or the next number in a series of random numbers.

Syntax

%result = $Random([minimum], [maximum], [%seed])

%result A numeric variable to receive an integer greater than or equal to minimum and less than or equal to maximum.
minimum The smallest value that will be returned; the minimum value of this argument is the same as its default: -1,000,000,000.
maximum The greatest value that will be returned; the maximum value of this argument is the same as its default: 1,000,000,000. This argument must be greater than the smallest value that will be returned.
%seed A %variable or IMAGE item that was set to a random number seed using $Random_Seed. This argument must be a string of length at least 144.

If this argument is omitted, the number returned is unpredictable; if it is present, the seed determines a specific series of random numbers, and each call to $Random returns the next number in that series.

If any argument is invalid, the request is cancelled.

Examples

  1. In the following example an unpredictable hand of 5 playing cards is "dealt".

    %I = 5 REPEAT WHILE %I GT 0 %N = $Random(1, 52) IF %CARD(%N) NE '' THEN PRINT %CARD(%N) %I = %I - 1 %CARD(%N) = '' END IF END REPEAT

  2. In the following example a hand of 5 playing cards is "dealt", and the "player" can either ask for the particular hand to play, or can just hit enter, and get an unpredictable hand.

    %HAND STRING LEN 144 REPEAT FOREVER %X = $READ('Pick a 4-digit hand number') IF %X EQ 0 THEN %X = $Random(1, 9999) END IF IF %X GT 0 AND %X LT 10000 THEN LOOP END END IF PRINT 'I said a 4 digit number!' END REPEAT PRINT 'You are playing hand number' AND %X %X = $Random_Seed(%HAND, %X) %I = 5 REPEAT WHILE %I GT 0 %N = $Random(1, 52, %HAND) IF %CARD(%N) NE '' THEN PRINT %CARD(%N) %I = %I - 1 %CARD(%N) = '' END IF END REPEAT

Products authorizing $Random