# Rand number options - a list?

**URL:** <https://mworks.discourse.group/t/rand-number-options-a-list/727>\
**Category:** Support\
**Created:** [July 13, 2010, 9:29pm UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727 "2010-07-13T21:29:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 13, 2010, 9:29pm UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727/1 "2010-07-13T21:29:23Z")

</div>

Hello Chris,

When you get a chance, could you please post the list of options for assigning random numbers with an “assign variable” as well as an explanation of exactly what they are doing? I am aware of:  
disc\_rand(min, max)  
geom\_rand(stair\_size, max\_val)  
I’m particularly interested in how draws from the geometric random distribution are determined (I believe there is a normalization stage to adjust for max\_val, but I’m not sure what it is).

Thanks,

Nicole

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 14, 2010, 9:54pm UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727/2 "2010-07-14T21:54:36Z")

</div>

Hi Nicole,

The MWorks expression parser supports three random number functions:

```
rand([low, high])
disc_rand(low, high)
geom_rand(Bernoulli_prob, high)

```

The first function, `rand`, returns a random floating-point value uniformly distributed in the range [low…high). If called without any parameters (e.g. `rand()`), low and high default to 0.0 and 1.0, respectively. (See [boost::uniform\_real](http://www.boost.org/doc/libs/1_40_0/libs/random/random-distributions.html#uniform_real).)

The second function, `disc_rand`, returns a random integer value uniformly distributed in the set of integer numbers {min, min+1, min+2, …, max}. (See [boost::uniform\_int](http://www.boost.org/doc/libs/1_40_0/libs/random/random-distributions.html#uniform_int).)

The third function, `geom_rand`, returns a discrete random number sampled in the interval [0, high] from a geometric distribution with constant Bernoulli probability Bernoulli\_prob. The comments in the code say it uses a Monte Carlo sampling algorithm taken from Rubinstein. The value is computed by the following code, where `uni()` is equivalent to `rand()`:

```
do {
    value = floor( log( uni() ) / log( 1-Bernoulli_prob ) );
} while ( value > high );

```

If you need more info, please let me know.

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 15, 2010, 7:51am UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727/3 "2010-07-15T07:51:18Z")

</div>

Hi Nicole,

as you know I implemented that function.

I can only add that, since “high” is the largest value that the  
sampled integer can take, the distribution is obviously not truly  
geometrical (otherwise it should allow sampled values up to  
+infinity). Therefore, the distribution is actually a truncated (to  
high) and re-normalized geometrical distribution. The renormalization  
simply means that in the interval [0 high] the probability to sample  
each integer is slightly higher than for a real geom. distribution, to  
take into account that all integer values \> high are not allowed.

I hope it can help  
davide

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 15, 2010, 9:31pm UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727/4 "2010-07-15T21:31:47Z")

</div>

Great - thanks to you both!

As a related reference, I see that Elias and Chris have a nice list of other functions (cos, tan, log) posted under the “Suggestions” thread.

Nicole

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 11:59pm UTC](https://mworks.discourse.group/t/rand-number-options-a-list/727/5 "2022-07-19T23:59:07Z")

</div>

Closed on July 19, 2022, 11:59pm UTC


