random: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "// since Arma 3" to "// since {{arma3}}")
m (Some wiki formatting)
Line 90: Line 90:
_array = ["apples", "pears", "bananas", "M16"];
_array = ["apples", "pears", "bananas", "M16"];


_random = _array select floor random count _array; // before Arma 2
_random = _array select floor random count _array; // before {{arma2}}
_random = _array call BIS_fnc_selectRandom; // since Arma 2
_random = _array call BIS_fnc_selectRandom; // since {{arma3}}
_random = selectRandom _array; // since {{arma3}} v1.56
_random = selectRandom _array; // since {{arma3}} v1.56
</sqf>
</sqf>
Line 98: Line 98:
<sqf>
<sqf>
private _parent = call BIS_fnc_displayMission;
private _parent = call BIS_fnc_displayMission;
if (is3DEN) then {_parent = findDisplay 313};
if (is3DEN) then { _parent = findDisplay 313 };
private _disp = _parent createDisplay "RscDisplayEmpty";
private _disp = _parent createDisplay "RscDisplayEmpty";
private _BG = _disp ctrlCreate ["RscBackground",-1];
private _BG = _disp ctrlCreate ["RscBackground", -1];


private _coef = 0.1;
private _coef = 0.1;


for "_x" from 1 to 100 do {
for "_x" from 1 to 100 do
for "_y" from 1 to 100 do {
{
private _random = 0 random [_x*_coef,_y*_coef];
for "_y" from 1 to 100 do
private _dot = _disp ctrlCreate ["RscBackground",-1];
{
private _random = 0 random [_x * _coef, _y * _coef];
_dot ctrlSetPosition [0.5+pixelW*_x,0.5+pixelH*_y,pixelW,pixelH];
private _dot = _disp ctrlCreate ["RscBackground", -1];
_dot ctrlSetBackgroundColor [_random,_random,_random,1];
 
_dot ctrlSetPosition [0.5 + pixelW * _x, 0.5 + pixelH * _y, pixelW, pixelH];
_dot ctrlSetBackgroundColor [_random, _random, _random, 1];
_dot ctrlCommit 0;
_dot ctrlCommit 0;
};
};
Line 117: Line 119:
[[File:A3 random syntax4.png|thumb|left|Example result]]
[[File:A3 random syntax4.png|thumb|left|Example result]]


|seealso= [[selectRandom]] [[selectRandomWeighted]]
|seealso= [[selectRandom]] [[selectRandomWeighted]] [[Example Code: Random Area Distribution]]
}}
}}


Line 124: Line 126:
|timestamp= 20150712203200
|timestamp= 20150712203200
|text= Random selections including negative numbers can be obtained via:
|text= Random selections including negative numbers can be obtained via:
<sqf>_Xrnd = round(random 200) -100;</sqf>
<sqf>_xRnd = round (random 200) - 100;</sqf>


This will yield numbers between -100 and 100.
This will yield numbers between -100 and 100.
Line 132: Line 134:
<sqf>
<sqf>
x = round (random 5) // will return 0, 1, 2, 3, 4 or 5. (non-uniform distribution, 0 and 5 are half as likely to be selected than any of the other numbers)
x = round (random 5) // will return 0, 1, 2, 3, 4 or 5. (non-uniform distribution, 0 and 5 are half as likely to be selected than any of the other numbers)
x = floor (random 5) // will return 0, 1, 2, 3 or 4.   (uniform distribution, all numbers have the same probability of being selected)
x = floor (random 5) // will return 0, 1, 2, 3 or 4.   (uniform distribution, all numbers have the same probability of being selected)
x = ceil  (random 5) // will return 0, 1, 2, 3, 4 or 5. (0 is very unlikely, but possible, as ceil 0 is 0)
x = ceil  (random 5) // will return 0, 1, 2, 3, 4 or 5. (0 is very unlikely, but possible, as ceil 0 is 0)
</sqf>
</sqf>

Revision as of 18:20, 18 November 2023

Hover & click on the images for description

Description

Description:
Syntax 2's distribution
Syntax 1 generates a random floating point value
Syntax 2 generates a Gaussian Distribution[1]. It uses the same method as setTriggerTimeout command and is quite useful for e.g spawning loot, making more valuable items more rare
Syntax 3 generates a seed-based random number
Syntax 4 generates a seed-based random noise (not Perlin) texture based on provided seed and returning the value at supplied coordinates
Groups:
Math

Syntax 1

Syntax:
random x
Parameters:
x: Number
Return Value:
Number from 0 (included) to x (excluded)

Syntax 2

Syntax:
random [min, mid, max]
Parameters:
min: Number
mid: Number
max: Number
Return Value:
Number in range from min to max, weighted by mid

Syntax 3

Syntax:
seed random x
Parameters:
seed: Number - an integer. Any float value will be truncated (e.g -0.9, -0.1, 0.1 or 0.9 will be read as 0)
x: Number
Return Value:
Number from 0 (included) to x (excluded)

Syntax 4

Syntax:
seed random [x, y]
Parameters:
seed: Number
x: Number - x texture position
y: Number - y texture position
Return Value:
Number from 0 (included) to 1 (excluded)

Examples

Example 1:
_rNumber = random 1;
Example 2:
_rNumber = random -10;
Example 3:
Generate a random position inside a circle (see also Example Code: Random Area Distribution)
_center getPos [_radius * sqrt random 1, random 360];
Example 4:
Select a random value from an array:
_array = ["apples", "pears", "bananas", "M16"]; _random = _array select floor random count _array; // before Arma 2 _random = _array call BIS_fnc_selectRandom; // since Arma 3 _random = selectRandom _array; // since Arma 3 v1.56
Example 5:
Visualization of Syntax 4 (warning, it takes a bit to execute to draw):
private _parent = call BIS_fnc_displayMission; if (is3DEN) then { _parent = findDisplay 313 }; private _disp = _parent createDisplay "RscDisplayEmpty"; private _BG = _disp ctrlCreate ["RscBackground", -1]; private _coef = 0.1; for "_x" from 1 to 100 do { for "_y" from 1 to 100 do { private _random = 0 random [_x * _coef, _y * _coef]; private _dot = _disp ctrlCreate ["RscBackground", -1]; _dot ctrlSetPosition [0.5 + pixelW * _x, 0.5 + pixelH * _y, pixelW, pixelH]; _dot ctrlSetBackgroundColor [_random, _random, _random, 1]; _dot ctrlCommit 0; }; };
Example result

Additional Information

See also:
selectRandom selectRandomWeighted Example Code: Random Area Distribution

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Hcpookie - c
Posted on Jul 12, 2015 - 20:32 (UTC)
Random selections including negative numbers can be obtained via:
_xRnd = round (random 200) - 100;
This will yield numbers between -100 and 100.
  • Be careful using random numbers in multiplayer, each client will come up with a different result. See multiplayer tutorials for more general information about locality.
  • The number returned is unlikely to be a whole number.
To return a whole number use either round, ceil or floor together with random:
x = round (random 5) // will return 0, 1, 2, 3, 4 or 5. (non-uniform distribution, 0 and 5 are half as likely to be selected than any of the other numbers) x = floor (random 5) // will return 0, 1, 2, 3 or 4. (uniform distribution, all numbers have the same probability of being selected) x = ceil (random 5) // will return 0, 1, 2, 3, 4 or 5. (0 is very unlikely, but possible, as ceil 0 is 0)
Lou Montana - c
Posted on Jul 22, 2018 - 12:58 (UTC)
Repartition comparison
Formula 0 1 2 3 4 5 6 7 8 9
floor random 10
10% 10% 10% 10% 10% 10% 10% 10% 10% 10%
floor random [0, 5, 10]
0% 2% 7% 17% 25% 25% 17% 7% 2% 0%
floor random [0, 10, 0]
0% 0% 1% 2% 5% 9% 14% 19% 24% 26%
floor random [0, 10, 5]
0% 0% 0% 1% 2% 5% 9% 16% 28% 38%
  1. Technically, it is a rescaled Bates distribution with n = 4. The distribution is split in two at its midpoint and scaled linearly such that its maximum lies at the specified midpoint.