RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Random and Scripting with examples

nerun

Sorceror
Utility.Random( 4, 8 );
Means a number between 4 and (4+7) (would be 4 to 11 because the 8 is from 0 to 7).
It means too that if the first number is 1 (as in Utility.Random( 1, 8 )), it will be always a number between 1 and the second number, because it is 1 and (1 + second number minus 1).

Utility.RandomBool();
Returns true or false.

Sometimes used as a chance of 50%:
Code:
if ( Utility.RandomBool() )
        PackItem( new MessageInABottle() );

Utility.Random(4);
Means 0 to 3 (0 to number -1)

Generally used as a switch:
Code:
switch ( Utility.Random( 4 ) )
{
        case 0: PackItem( new DaemonArms() ); break;
        case 1: PackItem( new DaemonChest() ); break;
        case 2: PackItem( new DaemonGloves() ); break;
        case 3: PackItem( new DaemonLegs() ); break;
}

Utility.RandomMinMax( 4, 8 );
Means 4 to 8 (including 4 and 8)

Generally used to choose a random amount between a range:
Code:
Item GemLoot = Loot.RandomGem();
GemLoot.Amount = Utility.Random( 1, 3 );
DropItem( GemLoot );

0.02 > Utility.RandomDouble()
Means a chance of 2%.
0.002 is 0.2%
0.02 is 2%
0.20 is 20%
1.0 is 100%
It occurs because RandomDouble() chooses a floating number between 0 and 1. So if 0.02 is > then random number, so occurs the desired hypothesis (2% of chance).

Generally it is used as:
Code:
if ( 0.5 > Utility.RandomDouble() ) // means 50% chance
        // do something here...

Utility.RandomList();
Chooses an item from a list.

Generally used to choose a random appearance to an item:
Code:
[Constructable]
public TreasureLevel2() : base( Utility.RandomList( 0xe3c, 0xE3E, 0x9a9, 0xe42, 0x9ab, 0xe40, 0xe7f, 0xe77 ) )
{
        // means appearance of: Large, Medium and Small Crate; Wooden, Metal and Metal Golden Chest; or Keg and Barrel
 
Top