Core Function Random

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
 
(11 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
Random( <expression>, <expression2> )
+
Random( <minValue>, <maxValue>, <flag> )
 
</pre>
 
</pre>
  
Line 7: Line 7:
 
Get a random number from the defined area.
 
Get a random number from the defined area.
  
=== expression ===
+
=== Parameters ===
  
A number, or a variable containing a number.
+
== minValue ==
 +
 
 +
Optional; A number, or a variable containing a number.
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
Line 15: Line 17:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== expression2 ===
+
== maxValue ==
  
A number, or a variable containing a number.
+
Optional; A number, or a variable containing a number.
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
$var2 = 33;
 
$var2 = 33;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== flag ==
 +
 +
Optional; Type of random number to generate.
 +
 +
<pre>
 +
0 = Generate an integer (Int32) random number and store return value as a Double
 +
1 = Generate an integer (Int32) random number and store return value as an Int32
 +
2 = Generate an integer (Int64) random number and store return value as an Int64
 +
3 = Generate an integer (Int64) random number and store return value as a Double
 +
</pre>
 +
 +
Default: Generate a random double number (min: 0, max: 1) and store return value as a Double
  
 
=== Return Value ===  
 
=== Return Value ===  
  
 
Success: Returns a random number.
 
Success: Returns a random number.
Failure: Does not return a random number (if an expression is not properly defined).
 
  
==== Some Examples ====
+
Failure: Returns null.
 +
 
 +
=== Remarks ===
 +
 
 +
If you only use the first or first two (or none) params then the generated value and its return will both by a Double not an integer to obtain an integer you must specifically set so in the flags.
 +
 
 +
Warning:
 +
 
 +
The lower bound is inclusive and the upper bound is exclusive.
 +
 
 +
That means that Random() will return the lower number, but it is guaranteed to never return the upper one.
 +
 
 +
This means if you wish to generate a random number from 1 to 3 you must request from 1 to 4.
 +
 
 +
The same goes if you wish to generate a random number from 0.1 to 0.3 you must request from 0.1 to 0.4.
 +
 
 +
====  Example ====
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">

Latest revision as of 12:38, 14 June 2015

Random( <minValue>, <maxValue>, <flag> )

Contents

Description

Get a random number from the defined area.

Parameters

minValue

Optional; A number, or a variable containing a number.

$var = 5;

maxValue

Optional; A number, or a variable containing a number.

$var2 = 33;

flag

Optional; Type of random number to generate.

0 = Generate an integer (Int32) random number and store return value as a Double
1 = Generate an integer (Int32) random number and store return value as an Int32
2 = Generate an integer (Int64) random number and store return value as an Int64
3 = Generate an integer (Int64) random number and store return value as a Double

Default: Generate a random double number (min: 0, max: 1) and store return value as a Double

Return Value

Success: Returns a random number.

Failure: Returns null.

Remarks

If you only use the first or first two (or none) params then the generated value and its return will both by a Double not an integer to obtain an integer you must specifically set so in the flags.

Warning:

The lower bound is inclusive and the upper bound is exclusive.

That means that Random() will return the lower number, but it is guaranteed to never return the upper one.

This means if you wish to generate a random number from 1 to 3 you must request from 1 to 4.

The same goes if you wish to generate a random number from 0.1 to 0.3 you must request from 0.1 to 0.4.

Example

$var = 5; // first expression
$var2 = 33; // second expression
$ran = Random($var, $var2); // function in variable
printf($ran); // print a random number from $ran
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox