# Generating random values with a selection variable

**URL:** <https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637>\
**Category:** Support\
**Created:** [April 22, 2011, 8:56pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637 "2011-04-22T20:56:09Z")\
**Posts on this page:** 7\
**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:** [April 22, 2011, 8:56pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/1 "2011-04-22T20:56:09Z")

</div>

Hi Arash and Najib,

I investigated the issue we were discussing some more.

It appears that negative numbers in a selection variable’s “values” list get converted to positive numbers. I don’t yet see where or why this happens, but I’ll figure it out and let you know.

As for Arash’s original question (i.e. how to use a selection variable to draw randomly from a finite set of values), I’ve attached an example that demonstrates one way to do it. Here are the key points:

- `nsamples` is set to the number of values in `values`
- `sampling_method` is “samples”
- While the number of draws is less than the number of values, you do “Next Selection” after each draw. When the number of draws equals the number of values, you do “Reset Selections”.

This technique works equally well for random\_with\_replacement and random\_without\_replacement. (The example demonstrates random\_without\_replacement.)

There are probably other ways to do this, but this approach seems relatively straightforward to me.

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:** [April 22, 2011, 8:56pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/2 "2011-04-22T20:56:09Z")

</div>

Attachment: [selection\_var.xml](https://mworks.discourse.group/uploads/short-url/rPZd4rzCQ6R8YYO8k3hA92uHTjS.xml) (2.13 KB)

---

<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:** [April 24, 2011, 9:35pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/3 "2011-04-24T21:35:30Z")

</div>

Thanks Chris. It works perfectly.

---

<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:** [April 25, 2011, 3:53pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/4 "2011-04-25T15:53:42Z")

</div>

> It appears that negative numbers in a selection variable’s “values” list get converted to positive numbers. I don’t yet see where or why this happens, but I’ll figure it out and let you know.

I figured it out. This is happening because the library routine we use to split up the “values” list treats all punctation characters (including “-”) as item delimiters. Thus “0,-1,2” is interpreted the same as “0,1,2”, and all the values end up positive.

I’ve opened up a ticket for this issue. It should be easy to fix.

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:** [October 3, 2012, 8:17pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/5 "2012-10-03T20:17:54Z")

</div>

Hi Arash and Najib,

The selection variable bug that caused negative values to be converted to positive values is now fixed. As of tonight’s nightly build (and the upcoming MWorks 0.5 release), you should be able to using negative values in the “values” list without any trouble.

Cheers,  
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:** [October 3, 2012, 8:58pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/6 "2012-10-03T20:58:25Z")

</div>

Thanks 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 19, 2022, 11:33pm UTC](https://mworks.discourse.group/t/generating-random-values-with-a-selection-variable/637/7 "2022-07-19T23:33:01Z")

</div>


