# Random Variables?

**URL:** <https://forum.choiceofgames.com/t/random-variables/19710>\
**Category:** ChoiceScript Help\
**Created:** [August 11, 2016, 2:46pm UTC](https://forum.choiceofgames.com/t/random-variables/19710 "2016-08-11T14:46:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebxrpg](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/ebxrpg/32/16862_2.png) [@ebxrpg](https://forum.choiceofgames.com/u/ebxrpg)\
**Post date:** [August 11, 2016, 2:46pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/1 "2016-08-11T14:46:05Z")

</div>

Is there a way to set random variables? For example, what I want to do is code a roulette game, and have it give a random amount of cash in return. Is this possible?

---

<div class="post-metadata">

**Author:** ![CitizenShawn](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/citizenshawn/32/1386_2.png) [@CitizenShawn](https://forum.choiceofgames.com/u/CitizenShawn)\
**Post date:** [August 11, 2016, 2:54pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/2 "2016-08-11T14:54:53Z")

</div>

You can use \*rand in a bunch of different ways, but it is buggy in that if you refresh the page (click “stats” and the return to the game) you can get a random number every time. It’s a way to “cheat.”

So for roulette, you could do this:

```
*rand color 1 2
*if color = 1
  *set red true
  *set black false
*if color = 2 
  *set red false
  *set black true
*rand number 1 100

*if red
  Your number is Red ${number}.
*if black
  Your number is Black ${number}.

```

But keep in mind this would refresh every single time you go to Stats screen. You can get around this by adding in a \*page\_break right before \*if red I think. EDIT: **Actually that’s not true,** the only way I’ve found to get around it is by switching scenes completely so it doesn’t recalculate \*rand every time. It’s very confusing, I think it’s a big headache for everyone, and that’s why they don’t recommend random number generators.

---

<div class="post-metadata">

**Author:** ![Lucid](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/lucid/32/39775_2.png) [@Lucid](https://forum.choiceofgames.com/u/Lucid)\
**Post date:** [August 11, 2016, 3:28pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/3 "2016-08-11T15:28:38Z")

</div>

If you get the rand earlier, it stops it. I’m not sure what it needs to ‘lock it’, but I’m sure a \*choice works. I thought a \*page\_break would too, but you say it doesn’t? Hmmmm.

I think I’ve asked this before: @dfabulich Is this just a bug when running it ourselves, or does it continue into published games via iOS and Android? Are there plans to squash it?

---

<div class="post-metadata">

**Author:** ![RETowers](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/retowers/32/21477_2.png) [@RETowers](https://forum.choiceofgames.com/u/RETowers)\
**Post date:** [August 11, 2016, 4:03pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/4 "2016-08-11T16:03:16Z")

</div>

1. Any page of separation between the \*rand and where it’s called works (i.e. \*choice, \*page\_break, \*finish). Basically, to put it into grossly simple terms, when CS generates a page, it remembers two things, the stats at the start of the page, and where the start of the page is.

When you go to the stats screen then return, it reruns the page from the top. That means rerunning any \*rand. So long as \*rand does not visibly affect any page on which it is run, no problem, but if it does, well, it redoes the \*rand, which affects what the page displays.

1. It continues to apps and it would be a large change to the way things are handled to get rid of (and it’s easy to work around).

2. \*rand makes games much more difficult to test which is why it’s recommend against (plus on the user end, it’s basically a massive intensive to just try and game by restarting which, best case scenario, is immersion breaking, worst case is frustration inducing).

---

<div class="post-metadata">

**Author:** ![CitizenShawn](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/citizenshawn/32/1386_2.png) [@CitizenShawn](https://forum.choiceofgames.com/u/CitizenShawn)\
**Post date:** [August 11, 2016, 4:42pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/5 "2016-08-11T16:42:36Z")

</div>

Okay, that’s interesting. I was playing around with it like that and couldn’t get it to work with page break, and logically I was right along with you, it just baffled me why it kept re-generating.

Or maybe I’m just remembering completely wrong and I was right the first time xD

Good luck with your Random!

---

<div class="post-metadata">

**Author:** ![ebxrpg](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/ebxrpg/32/16862_2.png) [@ebxrpg](https://forum.choiceofgames.com/u/ebxrpg)\
**Post date:** [August 11, 2016, 5:51pm UTC](https://forum.choiceofgames.com/t/random-variables/19710/6 "2016-08-11T17:51:28Z")

</div>

I will try the code in a bit, I have to go out but I can try it later.

Thank you all so much!
