Random: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
imported>PhilmASTErpLus
m Clarified text
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''[[int]] Random([[int]] number)'''
{{
#vardefine:name|random}}{{
#vardefine:return_type|int}}{{


Returns a random number below the integer given. (Includes 0)
FunctionPage|
name={{#var:name}}|


So, if you used
function1={{Function|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|int|range}}|
p1desc={{Pspan|range}} is the number of unique numbers to choose from|
}}|


<code>
function_description=Generates a random number, from a minimum of 0 to a maximum of {{pspan|range}}-1. Note that this function aborts with an error if you attempt to supply 1 for {{pspan|range}} (not that a number between 0 and 0 is all that random).|
random(5);
</code>


It will return a number from 0 up to 4.


Note: Must select a number value of at least 2, otherwise random will print an error and return void.
code1={{CodeSample|
{{Format}}
title=Code Sample|
description=Mimics the effects of rolling 2d6 (two standard 6-sided dice to you non-gamer-geeks).|
code=
<syntaxhighlight>
int die1 = 1 + random(6);
int die2 = 1 + random(6);
print( "You rolled a " + die1 + " and a " + die2 + ", for a total of " + to_string(die1+die2) );
</syntaxhighlight>}}|
}}
 
[[Category:Math and Numbers]]

Latest revision as of 01:35, 3 July 2010

Function Syntax

int random(int range )

  • range is the number of unique numbers to choose from

Generates a random number, from a minimum of 0 to a maximum of range-1. Note that this function aborts with an error if you attempt to supply 1 for range (not that a number between 0 and 0 is all that random).

Code Sample

Mimics the effects of rolling 2d6 (two standard 6-sided dice to you non-gamer-geeks).

int die1 = 1 + random(6);
int die2 = 1 + random(6);
print( "You rolled a " + die1 + " and a " + die2 + ", for a total of " + to_string(die1+die2) );