class Random
class Random
lib/random/random.tya:2
Random provides the random/Random standard library API.
Source
# Random provides the random/Random standard library API.
class Random
# Random.bool provides the random/Random standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
bool: probability ->
if probability == nil
probability = 0.5
Random().check_probability(probability)
random_float() < probability
# Random.check_probability provides the random/Random standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
check_probability: probability ->
if (
probability.class != Number or probability != probability or probability < 0 or probability > 1
)
raise error("random.bool: probability must be between 0.0 and 1.0")
# Random.check_sample_count provides the random/Random standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Int the resulting value.
check_sample_count: items, count ->
if count.class != Number or count != count or count != count.to_i()
raise error("random.sample: count must be an integer")
if count < 0 or count > items.len()
raise error("random.sample: count out of range")
# Random.choice provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
choice: items ->
n = items.len()
if n == 0
raise error("random.choice: empty array")
items[random_int(0, n - 1)]
# Random.copy_array provides the random/Random standard library operation.
# @param items Array items value.
# @return Array the resulting value.
copy_array: items ->
out = []
i = 0
while i < items.len()
out.push(items[i])
i = i + 1
out
# Random.float provides the random/Random standard library operation.
# @return Any the resulting value.
float: ->
random_float()
# Random.int provides the random/Random standard library operation.
# @param min Any min value.
# @param max Any max value.
# @return Any the resulting value.
int: min, max ->
random_int(min, max)
# Random.sample provides the random/Random standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Any the resulting value.
sample: items, count ->
Random().check_sample_count(items, count)
shuffled = Random().shuffle_copy(items)
out = []
i = 0
while i < count
out.push(shuffled[i])
i = i + 1
out
# Random.seed provides the random/Random standard library operation.
# @param value String value value.
# @return Any the resulting value.
seed: value ->
random_seed(value)
# Random.shuffle provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle: items ->
n = items.len()
i = n - 1
while i > 0
j = random_int(0, i)
tmp = items[i]
items[i] = items[j]
items[j] = tmp
i = i - 1
nil
# Random.shuffle_copy provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle_copy: items ->
copy = Random().copy_array(items)
Random().shuffle(copy)
copy
# Random.weight_total provides the random/Random standard library operation.
# @param weights Any weights value.
# @param label Any label value.
# @return Any the resulting value.
weight_total: weights, label ->
if weights.len() == 0
raise error(label + ": empty weights")
total = 0
i = 0
while i < weights.len()
weight = weights[i]
if weight.class != Number or weight != weight or weight < 0
raise error(label + ": weights must be finite non-negative numbers")
total = total + weight
i = i + 1
if total <= 0
raise error(label + ": at least one weight must be positive")
total
# Random.weighted_choice provides the random/Random standard library operation.
# @param items Array items value.
# @param weights Any weights value.
# @return Any the resulting value.
weighted_choice: items, weights ->
if items.len() != weights.len()
raise error("random.weighted_choice: items and weights length mismatch")
items[Random().weighted_index(weights)]
# Random.weighted_index provides the random/Random standard library operation.
# @param weights Any weights value.
# @return Int the resulting value.
weighted_index: weights ->
total = Random().weight_total(weights, "random.weighted_index")
pick = random_float() * total
sum = 0
i = 0
while i < weights.len()
sum = sum + weights[i]
if pick < sum
return i
i = i + 1
weights.len() - 1
Methods
bool
Random.bool(probability)
lib/random/random.tya:6
Random.bool provides the random/Random standard library operation.
Source
# Random.bool provides the random/Random standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
bool: probability ->
if probability == nil
probability = 0.5
Random().check_probability(probability)
random_float() < probability
check_probability
Random.check_probability(probability)
lib/random/random.tya:15
Random.check_probability provides the random/Random standard library operation.
Source
# Random.check_probability provides the random/Random standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
check_probability: probability ->
if (
probability.class != Number or probability != probability or probability < 0 or probability > 1
)
raise error("random.bool: probability must be between 0.0 and 1.0")
check_sample_count
Random.check_sample_count(items, count)
lib/random/random.tya:25
Random.check_sample_count provides the random/Random standard library operation.
Source
# Random.check_sample_count provides the random/Random standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Int the resulting value.
check_sample_count: items, count ->
if count.class != Number or count != count or count != count.to_i()
raise error("random.sample: count must be an integer")
if count < 0 or count > items.len()
raise error("random.sample: count out of range")
choice
Random.choice(items)
lib/random/random.tya:34
Random.choice provides the random/Random standard library operation.
Source
# Random.choice provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
choice: items ->
n = items.len()
if n == 0
raise error("random.choice: empty array")
items[random_int(0, n - 1)]
copy_array
Random.copy_array(items)
lib/random/random.tya:43
Random.copy_array provides the random/Random standard library operation.
Source
# Random.copy_array provides the random/Random standard library operation.
# @param items Array items value.
# @return Array the resulting value.
copy_array: items ->
out = []
i = 0
while i < items.len()
out.push(items[i])
i = i + 1
out
float
Random.float()
lib/random/random.tya:53
Random.float provides the random/Random standard library operation.
Source
# Random.float provides the random/Random standard library operation.
# @return Any the resulting value.
float: ->
random_float()
int
Random.int(min, max)
lib/random/random.tya:60
Random.int provides the random/Random standard library operation.
Source
# Random.int provides the random/Random standard library operation.
# @param min Any min value.
# @param max Any max value.
# @return Any the resulting value.
int: min, max ->
random_int(min, max)
sample
Random.sample(items, count)
lib/random/random.tya:67
Random.sample provides the random/Random standard library operation.
Source
# Random.sample provides the random/Random standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Any the resulting value.
sample: items, count ->
Random().check_sample_count(items, count)
shuffled = Random().shuffle_copy(items)
out = []
i = 0
while i < count
out.push(shuffled[i])
i = i + 1
out
seed
Random.seed(value)
lib/random/random.tya:80
Random.seed provides the random/Random standard library operation.
Source
# Random.seed provides the random/Random standard library operation.
# @param value String value value.
# @return Any the resulting value.
seed: value ->
random_seed(value)
shuffle
Random.shuffle(items)
lib/random/random.tya:86
Random.shuffle provides the random/Random standard library operation.
Source
# Random.shuffle provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle: items ->
n = items.len()
i = n - 1
while i > 0
j = random_int(0, i)
tmp = items[i]
items[i] = items[j]
items[j] = tmp
i = i - 1
nil
shuffle_copy
Random.shuffle_copy(items)
lib/random/random.tya:100
Random.shuffle_copy provides the random/Random standard library operation.
Source
# Random.shuffle_copy provides the random/Random standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle_copy: items ->
copy = Random().copy_array(items)
Random().shuffle(copy)
copy
weight_total
Random.weight_total(weights, label)
lib/random/random.tya:109
Random.weight_total provides the random/Random standard library operation.
Source
# Random.weight_total provides the random/Random standard library operation.
# @param weights Any weights value.
# @param label Any label value.
# @return Any the resulting value.
weight_total: weights, label ->
if weights.len() == 0
raise error(label + ": empty weights")
total = 0
i = 0
while i < weights.len()
weight = weights[i]
if weight.class != Number or weight != weight or weight < 0
raise error(label + ": weights must be finite non-negative numbers")
total = total + weight
i = i + 1
if total <= 0
raise error(label + ": at least one weight must be positive")
total
weighted_choice
Random.weighted_choice(items, weights)
lib/random/random.tya:128
Random.weighted_choice provides the random/Random standard library operation.
Source
# Random.weighted_choice provides the random/Random standard library operation.
# @param items Array items value.
# @param weights Any weights value.
# @return Any the resulting value.
weighted_choice: items, weights ->
if items.len() != weights.len()
raise error("random.weighted_choice: items and weights length mismatch")
items[Random().weighted_index(weights)]
weighted_index
Random.weighted_index(weights)
lib/random/random.tya:136
Random.weighted_index provides the random/Random standard library operation.
Source
# Random.weighted_index provides the random/Random standard library operation.
# @param weights Any weights value.
# @return Int the resulting value.
weighted_index: weights ->
total = Random().weight_total(weights, "random.weighted_index")
pick = random_float() * total
sum = 0
i = 0
while i < weights.len()
sum = sum + weights[i]
if pick < sum
return i
i = i + 1
weights.len() - 1