# About \*rand Function

**URL:** <https://forum.choiceofgames.com/t/about-rand-function/504>\
**Category:** ChoiceScript Help\
**Created:** [May 30, 2012, 3:08pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504 "2012-05-30T15:08:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mert](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Mert](https://forum.choiceofgames.com/u/Mert)\
**Post date:** [May 30, 2012, 3:08pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/1 "2012-05-30T15:08:49Z")

</div>

Hello to all.

I’m trying to make a battle system.  
Your enemies will hit you, and your health will decrease in random.  
So I’m using \*rand command.  
Ex: \*rand health 70 100  
Code works fine. But there is a problem.

Imagine that you are in the beginning of a battle.  
Your health is 100.  
The enemy hits you, and your health decreases between 70 and 100.  
Lets say “80 Health” lost.  
When my health drops to 80, I take a look at the stats screen and see that my health is now 80.  
When I say “NEXT” to see the actual battling scene, and then press Stats button again, my health decreases between 70 100 again.  
Now, my health is 74 and I chose nothing.  
“NOTE: My health is not going under 70”  
If I repeat this page switching thing, my health decreases to 88, 91, 72, all random numbers between 70 and 100.  
And all I do is just switching the screens.  
I think, everytime I open the scene, \*rand command starts function again.  
How can I solve this?  
I want to decrease the health just once.  
Thank you.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [May 30, 2012, 3:12pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/2 "2012-05-30T15:12:24Z")

</div>

Please post your code, you’ve probably put the function in the wrong place, but I can’t be sure without seeing it.  
Also you don’t want it to set your health to a random number between bla bla - You want it to MINUS a rand value between x and y from your currenthealth and then set that new value as currenthealth, else you actually have a chance of your health going up…

---

<div class="post-metadata">

**Author:** ![Mert](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Mert](https://forum.choiceofgames.com/u/Mert)\
**Post date:** [May 30, 2012, 3:15pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/3 "2012-05-30T15:15:41Z")

</div>

"You want it to MINUS a rand value between x and y from your currenthealth and then set that new value as currenthealth, else you actually have a chance of your health going up… "

Can you give me an example about this sentence?  
Thanks.

---

<div class="post-metadata">

**Author:** ![JimD](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/jimd/32/21672_2.png) [@JimD](https://forum.choiceofgames.com/u/JimD)\
**Post date:** [May 30, 2012, 3:22pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/4 "2012-05-30T15:22:05Z")

</div>

\*rand hitvalue 70 100  
\*set health - hitvalue

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [May 30, 2012, 4:43pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/5 "2012-05-30T16:43:05Z")

</div>

Yup, Jim’s got it. 🙂

Or would it be \*set health (health-hitvalue) ?

Either way, I hope that helps.

---

<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:** [May 30, 2012, 5:57pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/6 "2012-05-30T17:57:45Z")

</div>

First, this is a reported bug that CoG (currently) does not consider big enough to fix. To put it simply \*rand does not like the stats screen, and the stats screen does not like \*rand. What causes it, is that any time you pull up the stats screen, upon leaving it every \*function fires again. For most functions, this isn’t a problem. No matter how many times you “\*set foo + 5”, you’re always going to end up with foo being five more than it was at the start. That is not true with \*rand, which is the point. There are two ways to handle this.

1. Don’t use a stats screen. If you disable it, you don’t have to worry about it. You can always display the stats intermittently. This is easier, but really doesn’t fix the problem.

2. \*rand every variable you would need before you need them. Let me give you an example.

\*rand d10\_roll\_1 1 10  
\*goto battle

\*label battle  
\*page\_break  
\*set health - d10\_roll\_1

Now, there are two pages there, the one before the \*page\_break, and the one after it. If you access the stats screen on the first page, it rerolls d10\_roll\_1, however, as d10\_roll\_1 doesn’t actually do anything on that page, there is no effect. If you go to the next page, and access the stats screen, when you return, your health will remain at the same level, as the \*rand is not recalled, only the \*set.

So, the trick is to \*rand every die roll you’ll need before the battle begins, then call them up when actually in the fight. You could probably also set it up so that each turn of the battle not only executes the last set of commands, but also rolls up the next set too if you wanted to.

---

<div class="post-metadata">

**Author:** ![Mert](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Mert](https://forum.choiceofgames.com/u/Mert)\
**Post date:** [May 30, 2012, 11:07pm UTC](https://forum.choiceofgames.com/t/about-rand-function/504/7 "2012-05-30T23:07:29Z")

</div>

@Reaperoa

You used “d10\_roll\_1” variable.  
Can you tell me the default value of that variable in mygame.js file?

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [May 31, 2012, 1:50am UTC](https://forum.choiceofgames.com/t/about-rand-function/504/8 "2012-05-31T01:50:07Z")

</div>

You can set the default value to whatever you want it to be:

,d10\_roll\_1 = 5;

For example, would mean the default value is the integer/number value: 5.

---

<div class="post-metadata">

**Author:** ![Mert](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Mert](https://forum.choiceofgames.com/u/Mert)\
**Post date:** [May 31, 2012, 9:34am UTC](https://forum.choiceofgames.com/t/about-rand-function/504/9 "2012-05-31T09:34:33Z")

</div>

Thanks.
