Commit 9a4027d2 authored by twanvl's avatar twanvl

Fixed random_int and random_real functions; added random_boolean.

Split random_select into random_select and random_select_many.
parent fbffb931
...@@ -14,6 +14,7 @@ These functions are built into the program, other [[type:function]]s can be defi ...@@ -14,6 +14,7 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:abs]] Absolute value | [[fun:abs]] Absolute value
| [[fun:random_int]] Generate a random [[type:int]]. | [[fun:random_int]] Generate a random [[type:int]].
| [[fun:random_real]] Generate a random [[type:double]]. | [[fun:random_real]] Generate a random [[type:double]].
| [[fun:random_boolean]] Generate a random [[type:boolean]].
! Text manipulation <<< ! Text manipulation <<<
| [[fun:to_upper]] Convert a string to upper case, @"aBc" -> "ABC"@. | [[fun:to_upper]] Convert a string to upper case, @"aBc" -> "ABC"@.
...@@ -44,7 +45,8 @@ These functions are built into the program, other [[type:function]]s can be defi ...@@ -44,7 +45,8 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:sort_list]] Sort a list. | [[fun:sort_list]] Sort a list.
| [[fun:filter_list]] Filter a list, keeping only elements that match a predicate. | [[fun:filter_list]] Filter a list, keeping only elements that match a predicate.
| [[fun:random_shuffle]] Randomly shuffle a list. | [[fun:random_shuffle]] Randomly shuffle a list.
| [[fun:random_select]] Pick random elements from a list. | [[fun:random_select]] Pick a random element from a list.
| [[fun:random_select_many]] Pick multiple random elements from a list.
! Keywords <<< ! Keywords <<<
| [[fun:expand_keywords]] Expand the keywords in a piece of text. | [[fun:expand_keywords]] Expand the keywords in a piece of text.
......
Function: random_boolean
DOC_MSE_VERSION: since 0.3.8
--Usage--
> random_boolean(probability)
Returns a random [[type:bool]], either @true@ or @false.
The parameter is the probability of returning @true@, so @random_boolean(0.0)@ always returns @true@, @random_boolean(1.0)@ always returns @false@ and @random_boolean(0.5)@ simulates a fair coin toss.
Since the result is random, calling the function twice will give a different answer.
--Parameters--
! Parameter Type Default Description
| @input@ [[type:double]] @0.5@ Probability of returining true
--Examples--
> random_boolean(0.5) == true
> random_boolean(0.5) == false
--See also--
| [[fun:random_real]] Generate a random [[type:double]].
...@@ -23,3 +23,4 @@ Since the result is random, calling the function twice will give a different ans ...@@ -23,3 +23,4 @@ Since the result is random, calling the function twice will give a different ans
--See also-- --See also--
| [[fun:random_real]] Generate a random [[type:double]]. | [[fun:random_real]] Generate a random [[type:double]].
| [[fun:random_boolean]] Generate a random [[type:boolean]].
...@@ -22,3 +22,4 @@ Since the result is random, calling the function twice will give a different ans ...@@ -22,3 +22,4 @@ Since the result is random, calling the function twice will give a different ans
--See also-- --See also--
| [[fun:random_int]] Generate a random [[type:int|integer number]]. | [[fun:random_int]] Generate a random [[type:int|integer number]].
| [[fun:random_boolean]] Generate a random [[type:boolean]].
...@@ -5,28 +5,18 @@ DOC_MSE_VERSION: since 0.3.7 ...@@ -5,28 +5,18 @@ DOC_MSE_VERSION: since 0.3.7
--Usage-- --Usage--
> random_select(some_list, count: some_number, replace: some_boolean) > random_select(some_list, count: some_number, replace: some_boolean)
Randomly select a number of items from a list. Randomly select an items from a list.
If the @count@ parameter is not given, then a single item is picked at random.
Otherwise @count@ ''different'' items are selected (selection without replacment).
Setting the @replace@ parameter allows the same item to occur more than once in the result (selection with replacment).
Since the result is random, calling the function twice will give a different answer. Since the result is random, calling the function twice will give a different answer.
--Parameters-- --Parameters--
! Parameter Type Description ! Parameter Type Description
| @input@ [[type:list]] List to shuffle. | @input@ [[type:list]] List to shuffle.
| @count@ [[type:int]] Number of items to select.
| @replace@ [[type:boolean]] Select with replacement?
--Examples-- --Examples--
> random_select([1,2,3,4]) == 4 > random_select([1,2,3,4]) == 4
> random_select([1,2,3,4]) == 2 > random_select([1,2,3,4]) == 2
> random_select([1,2,3,4], count:3) == [2,3,1]
> random_select([1,2,3,4], count:3) == [3,1,4]
> random_select([1,2,3,4], count:3, replace: true) == [2,3,2]
> random_select([1,2,3,4], count:3, replace: false) == [1,3,4]
--See also-- --See also--
| [[fun:random_select_many]] Pick multiple random elements from a list.
| [[fun:random_shuffle]] Randomly shuffle a list. | [[fun:random_shuffle]] Randomly shuffle a list.
Function: random_select_many
DOC_MSE_VERSION: since 0.3.8
--Usage--
> random_select(some_list, count: some_number, replace: some_boolean)
Randomly select a number of items from a list.
By default @count@ ''different'' items are selected (selection without replacment).
Setting the @replace@ parameter allows the same item to occur more than once in the result (selection with replacment).
Since the result is random, calling the function twice will give a different answer.
--Parameters--
! Parameter Type Description
| @input@ [[type:list]] List to shuffle.
| @count@ [[type:int]] Number of items to select.
| @replace@ [[type:boolean]] Select with replacement?
--Examples--
> random_select_many([1,2,3,4], count:3) == [2,3,1]
> random_select_many([1,2,3,4], count:3) == [3,1,4]
> random_select_many([1,2,3,4], count:3, replace: true) == [2,3,2]
> random_select_many([1,2,3,4], count:3, replace: false) == [1,3,4]
--See also--
| [[fun:random_select]] Pick a single random element from a list.
| [[fun:random_shuffle]] Randomly shuffle a list.
...@@ -18,4 +18,5 @@ Since the result is random, calling the function twice will give a different ans ...@@ -18,4 +18,5 @@ Since the result is random, calling the function twice will give a different ans
> random_shuffle([1,2,3,4]) == [2,3,4,1] > random_shuffle([1,2,3,4]) == [2,3,4,1]
--See also-- --See also--
| [[fun:random_select]] Pick random elements from a list. | [[fun:random_select]] Pick a single random element from a list.
| [[fun:random_select_many]] Pick multiple random elements from a list.
...@@ -232,6 +232,11 @@ SCRIPT_FUNCTION(random_int) { ...@@ -232,6 +232,11 @@ SCRIPT_FUNCTION(random_int) {
SCRIPT_RETURN( rand() % (end - begin) + begin ); SCRIPT_RETURN( rand() % (end - begin) + begin );
} }
SCRIPT_FUNCTION(random_boolean) {
SCRIPT_PARAM_DEFAULT_C(double, input, 0.5);
SCRIPT_RETURN( rand() < RAND_MAX * input );
}
// ----------------------------------------------------------------------------- : String stuff // ----------------------------------------------------------------------------- : String stuff
// convert a string to upper case // convert a string to upper case
...@@ -521,40 +526,43 @@ SCRIPT_FUNCTION(random_shuffle) { ...@@ -521,40 +526,43 @@ SCRIPT_FUNCTION(random_shuffle) {
SCRIPT_FUNCTION(random_select) { SCRIPT_FUNCTION(random_select) {
SCRIPT_PARAM_C(ScriptValueP, input); SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_OPTIONAL_PARAM(int, count) { // pick a single one
// pick a single one int itemCount = input->itemCount();
int itemCount = input->itemCount(); if (itemCount == 0) {
throw ScriptError(_("Can not select a random item from an empty collection"));
}
return input->getIndex( rand() % itemCount );
}
SCRIPT_FUNCTION(random_select_many) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM(int, count) ;
SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, replace);
bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && (bool)*replace;
// pick many
ScriptCustomCollectionP ret(new ScriptCustomCollection);
int itemCount = input->itemCount();
if (with_replace) {
if (itemCount == 0) { if (itemCount == 0) {
throw ScriptError(String::Format(_("Can not select a random item from an empty collection"), count)); throw ScriptError(String::Format(_("Can not select %d items from an empty collection"), count));
}
for (int i = 0 ; i < count ; ++i) {
ret->value.push_back( input->getIndex( rand() % itemCount ) );
} }
return input->getIndex( rand() % itemCount );
} else { } else {
// pick many if (count > itemCount) {
SCRIPT_PARAM_DEFAULT_C(bool, replace, false); throw ScriptError(String::Format(_("Can not select %d items from a collection conaining only %d items"), count, input->itemCount()));
ScriptCustomCollectionP ret(new ScriptCustomCollection);
int itemCount = input->itemCount();
if (replace) {
if (itemCount == 0) {
throw ScriptError(String::Format(_("Can not select %d items from an empty collection"), count));
}
for (int i = 0 ; i < count ; ++i) {
ret->value.push_back( input->getIndex( rand() % itemCount ) );
}
} else {
if (count > itemCount) {
throw ScriptError(String::Format(_("Can not select %d items from a collection conaining only %d items"), count, input->itemCount()));
}
// transfer all to ret and shuffle
ScriptValueP it = input->makeIterator(input);
while (ScriptValueP v = it->next()) {
ret->value.push_back(v);
}
random_shuffle(ret->value.begin(), ret->value.end());
// keep only the first 'count'
ret->value.resize(count);
} }
return ret; // transfer all to ret and shuffle
ScriptValueP it = input->makeIterator(input);
while (ScriptValueP v = it->next()) {
ret->value.push_back(v);
}
random_shuffle(ret->value.begin(), ret->value.end());
// keep only the first 'count'
ret->value.resize(count);
} }
return ret;
} }
// ----------------------------------------------------------------------------- : Keywords // ----------------------------------------------------------------------------- : Keywords
...@@ -640,8 +648,9 @@ void init_script_basic_functions(Context& ctx) { ...@@ -640,8 +648,9 @@ void init_script_basic_functions(Context& ctx) {
ctx.setVariable(_("to code"), script_to_code); ctx.setVariable(_("to code"), script_to_code);
// math // math
ctx.setVariable(_("abs"), script_abs); ctx.setVariable(_("abs"), script_abs);
ctx.setVariable(_("random_real"), script_random_real); ctx.setVariable(_("random real"), script_random_real);
ctx.setVariable(_("random_int"), script_random_int); ctx.setVariable(_("random int"), script_random_int);
ctx.setVariable(_("random boolean"), script_random_boolean);
// string // string
ctx.setVariable(_("to upper"), script_to_upper); ctx.setVariable(_("to upper"), script_to_upper);
ctx.setVariable(_("to lower"), script_to_lower); ctx.setVariable(_("to lower"), script_to_lower);
...@@ -670,6 +679,7 @@ void init_script_basic_functions(Context& ctx) { ...@@ -670,6 +679,7 @@ void init_script_basic_functions(Context& ctx) {
ctx.setVariable(_("sort list"), script_sort_list); ctx.setVariable(_("sort list"), script_sort_list);
ctx.setVariable(_("random shuffle"), script_random_shuffle); ctx.setVariable(_("random shuffle"), script_random_shuffle);
ctx.setVariable(_("random select"), script_random_select); ctx.setVariable(_("random select"), script_random_select);
ctx.setVariable(_("random select many"), script_random_select_many);
// keyword // keyword
ctx.setVariable(_("expand keywords"), script_expand_keywords); ctx.setVariable(_("expand keywords"), script_expand_keywords);
ctx.setVariable(_("expand keywords rule"), new_intrusive1<ScriptRule>(script_expand_keywords)); ctx.setVariable(_("expand keywords rule"), new_intrusive1<ScriptRule>(script_expand_keywords));
......
...@@ -15,6 +15,7 @@ $built_in_functions = array( ...@@ -15,6 +15,7 @@ $built_in_functions = array(
'abs' =>'', 'abs' =>'',
'random_int' =>'', 'random_int' =>'',
'random_real' =>'', 'random_real' =>'',
'random_boolean' =>'',
// text // text
'to_upper' =>'', 'to_upper' =>'',
'to_lower' =>'', 'to_lower' =>'',
...@@ -43,6 +44,7 @@ $built_in_functions = array( ...@@ -43,6 +44,7 @@ $built_in_functions = array(
'filter_list' =>'', 'filter_list' =>'',
'random_shuffle' =>'', 'random_shuffle' =>'',
'random_select' =>'', 'random_select' =>'',
'random_select_many' =>'',
// keywords // keywords
'expand_keywords' =>'', 'expand_keywords_rule'=>'expand_keywords', 'expand_keywords' =>'', 'expand_keywords_rule'=>'expand_keywords',
'keyword_usage' =>'', 'keyword_usage' =>'',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment