class Rng
class Rng
lib/random/rng.tya:2
Rng provides the random/Rng standard library API.
Source
# Rng provides the random/Rng standard library API.
class Rng
# Rng.state stores instance state.
# @type Int
state: 1
# Rng.initialize provides the random/Rng standard library operation.
# @param seed Any seed value.
# @return Self the initialized object.
initialize: seed ->
self.state = 1
if seed.class != Number or seed != seed
raise error("random.Rng.seed: seed must be a number")
self.state = seed.to_i() % 2147483647
if self.state <= 0
self.state = self.state + 2147483646
# Rng.bool provides the random/Rng standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
bool: probability ->
if probability == nil
probability = 0.5
self.check_probability(probability)
self.float() < probability
# Rng.check_probability provides the random/Rng 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.Rng.bool: probability must be between 0.0 and 1.0")
# Rng.check_sample_count provides the random/Rng 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.to_i() or count != count
raise error("random.Rng.sample: count must be an integer")
if count < 0 or count > items.len()
raise error("random.Rng.sample: count out of range")
# Rng.choice provides the random/Rng standard library operation.
# @param items Array items value.
# @return Any the resulting value.
choice: items ->
n = items.len()
if n == 0
raise error("random.Rng.choice: empty array")
items[self.int(0, n - 1)]
# Rng.copy_array provides the random/Rng 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
# Rng.float provides the random/Rng standard library operation.
# @return Any the resulting value.
float: ->
self.next_int() / 2147483647
# Rng.int provides the random/Rng standard library operation.
# @param min Any min value.
# @param max Any max value.
# @return Any the resulting value.
int: min, max ->
if (
min.class != Number or max.class != Number or min != min.to_i() or max != max.to_i()
)
raise error("random.Rng.int: bounds must be integers")
if max < min
raise error("random.Rng.int: max < min")
span = max - min + 1
min + self.next_int() % span
# Rng.new provides the random/Rng standard library operation.
# @param seed Any seed value.
# @return Self the resulting value.
new: seed ->
Rng(seed)
# Rng.next_int provides the random/Rng standard library operation.
# @return Any the resulting value.
next_int: ->
self.state = self.state * 48271 % 2147483647
self.state
# Rng.sample provides the random/Rng standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Any the resulting value.
sample: items, count ->
self.check_sample_count(items, count)
shuffled = self.shuffle_copy(items)
out = []
i = 0
while i < count
out.push(shuffled[i])
i = i + 1
out
# Rng.seed provides the random/Rng standard library operation.
# @param value String value value.
# @return Any the resulting value.
seed: value ->
if value.class != Number or value != value
raise error("random.Rng.seed: seed must be a number")
self.state = value.to_i() % 2147483647
if self.state <= 0
self.state = self.state + 2147483646
self
# Rng.shuffle provides the random/Rng 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 = self.int(0, i)
tmp = items[i]
items[i] = items[j]
items[j] = tmp
i = i - 1
nil
# Rng.shuffle_copy provides the random/Rng standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle_copy: items ->
copy = self.copy_array(items)
self.shuffle(copy)
copy
# Rng.weight_total provides the random/Rng 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
# Rng.weighted_choice provides the random/Rng 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.Rng.weighted_choice: items and weights length mismatch")
items[self.weighted_index(weights)]
# Rng.weighted_index provides the random/Rng standard library operation.
# @param weights Any weights value.
# @return Int the resulting value.
weighted_index: weights ->
total = self.weight_total(weights, "random.Rng.weighted_index")
pick = self.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
Instance Variables
state
Rng.state
lib/random/rng.tya:5
Rng.state stores instance state.
Source
# Rng.state stores instance state.
# @type Int
state: 1
Methods
bool
Rng.bool(probability)
lib/random/rng.tya:21
Rng.bool provides the random/Rng standard library operation.
Source
# Rng.bool provides the random/Rng standard library operation.
# @param probability Any probability value.
# @return Any the resulting value.
bool: probability ->
if probability == nil
probability = 0.5
self.check_probability(probability)
self.float() < probability
check_probability
Rng.check_probability(probability)
lib/random/rng.tya:30
Rng.check_probability provides the random/Rng standard library operation.
Source
# Rng.check_probability provides the random/Rng 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.Rng.bool: probability must be between 0.0 and 1.0")
check_sample_count
Rng.check_sample_count(items, count)
lib/random/rng.tya:40
Rng.check_sample_count provides the random/Rng standard library operation.
Source
# Rng.check_sample_count provides the random/Rng 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.to_i() or count != count
raise error("random.Rng.sample: count must be an integer")
if count < 0 or count > items.len()
raise error("random.Rng.sample: count out of range")
choice
Rng.choice(items)
lib/random/rng.tya:49
Rng.choice provides the random/Rng standard library operation.
Source
# Rng.choice provides the random/Rng standard library operation.
# @param items Array items value.
# @return Any the resulting value.
choice: items ->
n = items.len()
if n == 0
raise error("random.Rng.choice: empty array")
items[self.int(0, n - 1)]
copy_array
Rng.copy_array(items)
lib/random/rng.tya:58
Rng.copy_array provides the random/Rng standard library operation.
Source
# Rng.copy_array provides the random/Rng 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
Rng.float()
lib/random/rng.tya:68
Rng.float provides the random/Rng standard library operation.
Source
# Rng.float provides the random/Rng standard library operation.
# @return Any the resulting value.
float: ->
self.next_int() / 2147483647
initialize
Rng.initialize(seed)
lib/random/rng.tya:10
Rng.initialize provides the random/Rng standard library operation.
Source
# Rng.initialize provides the random/Rng standard library operation.
# @param seed Any seed value.
# @return Self the initialized object.
initialize: seed ->
self.state = 1
if seed.class != Number or seed != seed
raise error("random.Rng.seed: seed must be a number")
self.state = seed.to_i() % 2147483647
if self.state <= 0
self.state = self.state + 2147483646
int
Rng.int(min, max)
lib/random/rng.tya:75
Rng.int provides the random/Rng standard library operation.
Source
# Rng.int provides the random/Rng standard library operation.
# @param min Any min value.
# @param max Any max value.
# @return Any the resulting value.
int: min, max ->
if (
min.class != Number or max.class != Number or min != min.to_i() or max != max.to_i()
)
raise error("random.Rng.int: bounds must be integers")
if max < min
raise error("random.Rng.int: max < min")
span = max - min + 1
min + self.next_int() % span
new
Rng.new(seed)
lib/random/rng.tya:88
Rng.new provides the random/Rng standard library operation.
Source
# Rng.new provides the random/Rng standard library operation.
# @param seed Any seed value.
# @return Self the resulting value.
new: seed ->
Rng(seed)
next_int
Rng.next_int()
lib/random/rng.tya:93
Rng.next_int provides the random/Rng standard library operation.
Source
# Rng.next_int provides the random/Rng standard library operation.
# @return Any the resulting value.
next_int: ->
self.state = self.state * 48271 % 2147483647
self.state
sample
Rng.sample(items, count)
lib/random/rng.tya:101
Rng.sample provides the random/Rng standard library operation.
Source
# Rng.sample provides the random/Rng standard library operation.
# @param items Array items value.
# @param count Int count value.
# @return Any the resulting value.
sample: items, count ->
self.check_sample_count(items, count)
shuffled = self.shuffle_copy(items)
out = []
i = 0
while i < count
out.push(shuffled[i])
i = i + 1
out
seed
Rng.seed(value)
lib/random/rng.tya:114
Rng.seed provides the random/Rng standard library operation.
Source
# Rng.seed provides the random/Rng standard library operation.
# @param value String value value.
# @return Any the resulting value.
seed: value ->
if value.class != Number or value != value
raise error("random.Rng.seed: seed must be a number")
self.state = value.to_i() % 2147483647
if self.state <= 0
self.state = self.state + 2147483646
self
shuffle
Rng.shuffle(items)
lib/random/rng.tya:125
Rng.shuffle provides the random/Rng standard library operation.
Source
# Rng.shuffle provides the random/Rng 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 = self.int(0, i)
tmp = items[i]
items[i] = items[j]
items[j] = tmp
i = i - 1
nil
shuffle_copy
Rng.shuffle_copy(items)
lib/random/rng.tya:139
Rng.shuffle_copy provides the random/Rng standard library operation.
Source
# Rng.shuffle_copy provides the random/Rng standard library operation.
# @param items Array items value.
# @return Any the resulting value.
shuffle_copy: items ->
copy = self.copy_array(items)
self.shuffle(copy)
copy
weight_total
Rng.weight_total(weights, label)
lib/random/rng.tya:148
Rng.weight_total provides the random/Rng standard library operation.
Source
# Rng.weight_total provides the random/Rng 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
Rng.weighted_choice(items, weights)
lib/random/rng.tya:167
Rng.weighted_choice provides the random/Rng standard library operation.
Source
# Rng.weighted_choice provides the random/Rng 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.Rng.weighted_choice: items and weights length mismatch")
items[self.weighted_index(weights)]
weighted_index
Rng.weighted_index(weights)
lib/random/rng.tya:175
Rng.weighted_index provides the random/Rng standard library operation.
Source
# Rng.weighted_index provides the random/Rng standard library operation.
# @param weights Any weights value.
# @return Int the resulting value.
weighted_index: weights ->
total = self.weight_total(weights, "random.Rng.weighted_index")
pick = self.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