$Random: Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
mNo edit summary
Line 2: Line 2:
<span class="pageSubtitle">Get next random number</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 the [[RandomNumberGenerator class]].</p>
<p class="warning">Most Sirius $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.  
Line 15: Line 15:


==Syntax==
==Syntax==
<p class="syntax"><span class="term">%result</span> = <span class="literal">$Random</span>(<span class="term">minimum, maximum, %SEED)
<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>
</p>
<p>
<p>
<var class="term">%result</var> contains a random number greater than or equal to '''minimum''' and less than or equal to '''maximum'''.</p>
If any argument is invalid, the request is cancelled.
==Examples==
<ol>
<li>In the following example an unpredictable hand of 5 playing cards is "dealt".
<p class="code">%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
</p>
</p>
<p>%result contains a random number greater than or equal to '''minimum''' and less than or equal to '''maximum'''.</p>


If any argument is invalid, the request is cancelled.  
<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.


In the following example
<p class="code">%HAND STRING LEN 144
<p class="code"> %I = 5
REPEAT FOREVER
REPEAT WHILE %I GT 0
  %X = $READ('Pick a 4-digit hand number')
   %N = $Random(1, 52)
  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
   IF %CARD(%N) NE '' THEN
       PRINT %CARD(%N)
       PRINT %CARD(%N)
Line 32: Line 60:
       %CARD(%N) = ''
       %CARD(%N) = ''
   END IF
   END IF
END REPEAT
END REPEAT
</p>
</p>
 
</ol>
an unpredictable hand of 5 playing cards is "dealt".
 
In the following example
<p class="code"> %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
</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.


==Products authorizing {{PAGENAMEE}}==  
==Products authorizing {{PAGENAMEE}}==  
Line 76: Line 76:


</ul>
</ul>
<p>
</p>


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

Revision as of 18:24, 11 April 2013

Get next random number

Most Sirius $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.

$Random accepts three optional arguments and returns an integer number.

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.

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.

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.

Syntax

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

%result contains a random number greater than or equal to minimum and less than or equal to maximum.

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