Předmět Autor Datum
Description random() generates a random real number between 0 and less than x. It is produced as a d… poslední
Masher 05.02.2015 12:42
Masher

Description

random() generates a random real number between 0 and less than x. It is produced as a decimal fraction, so if you want a whole number (integer), you should use floor() or ceil() in combination with random(). floor() is used most often.

Examples

Simple Example

After the code below is executed, rnum} will contain a random real number between 0 and less than 5. Remember, this number will be a number with a decimal point and not an integer.

rnum = random(5)

50/50 Chance

A 50/50 chance can be simulated this way :

if (random(1) >= .5)
{
//something happens
}

Simulating Dice Rolls

Standard 6-sided dice and other polyhedral dice rolls can be simulated by simply using random() to generate a number between 1 and the maximum number of the die. Don't forget the + 1.

die = floor(random(6)) + 1

Zpět do poradny Odpovědět na původní otázku Nahoru