# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2018-05-12 12:09:47

Member
Registered: 2018-05-12
Posts: 2

### Weighted random number

This tune-able script can return a weighted random number in 4 different ways:

1. Return the actual result
2. Return 1 if rare
3. Return 1 if average
4. Return 1 if common

There are 4 arguments to pass to it:

1. argument0 supplies a range to weight results to. This can be from 0 to 49 (is protected in the script)
2. argument1 states how many times to try to weight the result between argument0's full range
3. argument2 states how many times to try to weight the result between 1/3 and 2/3 argument0's range
4. argument3 states what you want returned from the script.

Expand///lumpyRandom(tier,repit,repit2,retType);
//
//Returns your choice of a weighted random number, or returns 1 if rare, average or common via argument3.
//
//    Arguments:
//    tier    real, the initial range to weight result to. minimum 0, max 49
//    repit    real, how many times to try to weight the result between tier's full range
//    repit2    real, how many times to try to weight the result between 1/3 and 2/3 tier's range
//    retType    real, if -1 return the result number, if 0 return 1 if rare, if 1 return 1 if average, if 2 return 1 if common
//
//NOTE: The closer to 0 tier is, the more accurate the results are as commented below.
//The closer to 49 tier is, the results become the opposite.
//When tier is close to 25 results are more neutral.
//
{

var tier,repit,repit2,retType,a,b,tries,tries2,results;
tier = argument0;
repit = argument1;
repit2 = argument2;
retType = argument3;
b = -1;
tries = 0;
tries2 = 0;
results = 50;

if tier < 0 tier = 0;
if tier > 49 tier = 49; //Protected for script integrity

a = round(random(100));

while ((a < tier || a >= 100 - tier) && tries < repit) {
tries += 1;
a = round(random(100));
}

if (a >= tier && a < 100 - tier) {
while ((b < (((100 - tier) - tier) / 3) + tier
|| b >= 100 - tier - (((100 - tier) - tier) / 3)) && tries2 < repit2) {
tries2 += 1;
b = tier + round(random(100 - tier));
}
}

if b = -1
results = a;
if b != -1
results =  b;

if(retType == -1) {//just return the random number
return results;
}

if(retType == 0) {//rare (see note above)
if results < tier || results >= 100 - tier
return 1;
return 0;
}

if(retType == 1) {//average (see note above)
if (results >= tier && results < (((100 - tier) - tier) / 3) + tier)
|| (results < 100 - tier && results > 100 - tier - (((100 - tier) - tier) / 3))
return 1;
return 0;
}

if(retType == 2) {//common (see note above)
if results >= (((100 - tier) - tier) / 3) + tier && results < 100 - tier - (((100 - tier) - tier) / 3)
return 1;
return 0;
}

}

Usage:
example 1, get a weighted number

Expandvar rand = lumpyRandom(10,1,1,-1);
repeat (rand) {
instance_create(random(200), random(200), enemy);
}

example 2, do something if result is rare

Expandif  (lumpyRandom(3,2,2,0) == 1) {
global.foundLegendary[global.chapter] = 1;
}

example 3, do something if result is average

Expandif  (lumpyRandom(3,2,0,1) == 1) {
player1.strength += 5;
}

example 4, do something if result is common

Expandif  (lumpyRandom(3,2,2,2) == 1) {
instance_create(player1.x, player1.y - 50, commonTreasure);
}

Offline