$Random: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
mNo edit summary
Line 2: Line 2:
<span class="pageSubtitle"><section begin="desc" />Get next random number<section end="desc" /></span>
<span class="pageSubtitle"><section begin="desc" />Get next random number<section end="desc" /></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="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>


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 13: Line 13:


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.
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==
==Syntax==
<p class="syntax"><section begin="syntax" /> %RESULT = $Random(minimum, maximum, %SEED)
<p class="syntax"><section begin="syntax" /> %RESULT = $Random(minimum, maximum, %SEED)
Line 19: Line 20:
</p>
</p>
<p class="caption">%RESULT contains a random number greater than or equal to '''minimum''' and less than or equal to '''maximum'''.</p>
<p class="caption">%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.  
If any argument is invalid, the request is cancelled.  
Line 26: Line 26:
<p class="code"> %I = 5
<p class="code"> %I = 5
  REPEAT WHILE %I GT 0
  REPEAT WHILE %I GT 0
%N = $Random(1, 52)
  %N = $Random(1, 52)
IF %CARD(%N) NE '' THEN
  IF %CARD(%N) NE '' THEN
PRINT %CARD(%N)
      PRINT %CARD(%N)
%I = %I - 1
      %I = %I - 1
%CARD(%N) = ''
      %CARD(%N) = ''
END IF
  END IF
  END REPEAT
  END REPEAT
</p>
</p>
an unpredictable hand of 5 playing cards is "dealt".  
an unpredictable hand of 5 playing cards is "dealt".  


Line 39: Line 40:
<p class="code"> %HAND STRING LEN 144
<p class="code"> %HAND STRING LEN 144
  REPEAT FOREVER
  REPEAT FOREVER
%X = $READ('Pick a 4-digit hand number')
    %X = $READ('Pick a 4-digit hand number')
IF %X EQ 0 THEN
    IF %X EQ 0 THEN
%X = $Random(1, 9999)
      %X = $Random(1, 9999)
END IF
    END IF
IF %X GT 0 AND %X LT 10000 THEN
    IF %X GT 0 AND %X LT 10000 THEN
LOOP END
      LOOP END
END IF
    END IF
PRINT 'I said a 4 digit number!'
    PRINT 'I said a 4 digit number!'
  END REPEAT
  END REPEAT
  PRINT 'You are playing hand number' AND %X
  PRINT 'You are playing hand number' AND %X
Line 52: Line 53:
  %I = 5
  %I = 5
  REPEAT WHILE %I GT 0
  REPEAT WHILE %I GT 0
%N = $Random(1, 52, %HAND)
    %N = $Random(1, 52, %HAND)
IF %CARD(%N) NE '' THEN
    IF %CARD(%N) NE '' THEN
PRINT %CARD(%N)
      PRINT %CARD(%N)
%I = %I - 1
      %I = %I - 1
%CARD(%N) = ''
      %CARD(%N) = ''
END IF
    END IF
  END REPEAT
  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.
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.
<ul class="smallAndTightList">
<ul class="smallAndTightList">
<li>[[Sirius functions]]</li>
<li>[[Sirius functions]]</li>
Line 74: Line 77:
<p class="caption">Products authorizing $Random
<p class="caption">Products authorizing $Random
</p>
</p>


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

Revision as of 20:48, 2 February 2011

<section begin="desc" />Get next random number<section end="desc" />

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

<section begin="syntax" /> %RESULT = $Random(minimum, maximum, %SEED) <section end="syntax" />

$Random Function

%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.

In the following example

%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

an unpredictable hand of 5 playing cards is "dealt".

In the following example

%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

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 $Random