# Using \*rand with a text variable

**URL:** <https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629>\
**Category:** ChoiceScript Help\
**Created:** [August 8, 2016, 4:53am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629 "2016-08-08T04:53:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eleckar](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eleckar/32/72266_2.png) [@Eleckar](https://forum.choiceofgames.com/u/Eleckar)\
**Post date:** [August 8, 2016, 4:53am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/1 "2016-08-08T04:53:03Z")

</div>

Is there a way to use \*rand with a text variable and a number instead of two numbers?  
`*rand blah 1 "Variable"`

(“Variable” is a variable that holds a number defined previously by the user using \*input\_number)

Thank you!

---

<div class="post-metadata">

**Author:** ![Lordirish](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/lordirish/32/238051_2.png) [@Lordirish](https://forum.choiceofgames.com/u/Lordirish)\
**Post date:** [August 8, 2016, 5:28am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/2 "2016-08-08T05:28:14Z")

</div>

Not sure what you want exactly but rand requires two numbers to defind what it is rolling such as 1 5 or 20 30. As far as using text var I have seen it in if only so far, of course I am not good with the updated code

```
*rand var 1 5
*if (var=5)
 *set text_var "something"
 *goto some_place
*if (rand=4)
 *set text_var "something_else"
 *goto some_place
```

---

<div class="post-metadata">

**Author:** ![Eleckar](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eleckar/32/72266_2.png) [@Eleckar](https://forum.choiceofgames.com/u/Eleckar)\
**Post date:** [August 8, 2016, 5:38am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/3 "2016-08-08T05:38:14Z")

</div>

Alright, thanks.

It’s too bad, really; that’ll limit my program exponentially.

Edit: Never mind. That doesn’t make any sense.

---

<div class="post-metadata">

**Author:** ![Lordirish](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/lordirish/32/238051_2.png) [@Lordirish](https://forum.choiceofgames.com/u/Lordirish)\
**Post date:** [August 8, 2016, 5:45am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/4 "2016-08-08T05:45:45Z")

</div>

What exactly do you want to happen as there many ways to tackle a problem in CS and someone may have a better answer for you 😄

---

<div class="post-metadata">

**Author:** ![Eleckar](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eleckar/32/72266_2.png) [@Eleckar](https://forum.choiceofgames.com/u/Eleckar)\
**Post date:** [August 8, 2016, 5:56am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/5 "2016-08-08T05:56:19Z")

</div>

Alright, so.

That dice game I published a couple of days ago. I originally wanted for the player to be able to input a crazy number and watch it get multiplied and randomized by another number they entered.

The player inputs one number. It is the number of dice being rolled.

Then s/he/it inputs another number. It is the number of faces each die has.

Each die’s number is randomized by the number of sides, then added to form a sum dependent on the number of total dice.

* * *

However, I’ve found another solution. I’m just going to have a \*choice between the most commonly used variants of dice that I found here [https://en.wikipedia.org/wiki/Dice#Variants](https://en.wikipedia.org/wiki/Dice#Variants)

---

<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:** [August 8, 2016, 9:29am UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/6 "2016-08-08T09:29:03Z")

</div>

> [@Eleckar](#):
>
> \*rand blah 1 “Variable”

I’m pretty sure you can pass variables for the min and max values of rand, if that’s what you’re asking?

---

<div class="post-metadata">

**Author:** ![Eleckar](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eleckar/32/72266_2.png) [@Eleckar](https://forum.choiceofgames.com/u/Eleckar)\
**Post date:** [August 8, 2016, 4:26pm UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/7 "2016-08-08T16:26:46Z")

</div>

Yes. How would I go about doing this?

---

<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 8, 2016, 8:20pm UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/8 "2016-08-08T20:20:20Z")

</div>

I believe

\*Rand var 1 6

would generate a number between 1 and 6. You could use a gosub to roll that dice as many times as the user specifies. I have a dice mechanic that will generate creature stats based on the hit dice (sides) and the number of dice (determined by level).

So here is my code to generate a random amount of hit points for an enemy:

```
Level ${enemy_level} ${enemy_description} (${hit_dice_count}d${enemy_hit_dice_sides})

*label health_gen
*if hit_dice_count > 0
	*set hit_dice_result 0
	*rand hit_dice_result 1 enemy_hit_dice_sides
	*set enemy_health + hit_dice_result
	The roll for die ${hit_dice_count} is ${hit_dice_result}.
	*set hit_dice_count - 1
	*goto health_gen
The ${enemy_description}'s base health is ${enemy_hit_dice_sides}. The ${enemy_description}'s total health is ${enemy_health}. 

```

So this would read:

Level 3 Goblin (3d8)  
The roll for die 3 is 6.  
The roll for die 2 is 7.  
The roll for die 1 is 5.  
The goblin’s base health is 8. The Goblin’s total health is 26.

(That’s 8+6+7+5)

---

<div class="post-metadata">

**Author:** ![Eleckar](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eleckar/32/72266_2.png) [@Eleckar](https://forum.choiceofgames.com/u/Eleckar)\
**Post date:** [August 8, 2016, 8:34pm UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/9 "2016-08-08T20:34:44Z")

</div>

Yeah. That’s pretty much what I ended up doing.

 ![](https://global.discourse-cdn.com/choiceofgames/original/3X/9/2/928a786c0b0870ca08d99cbff0366bc69ceed986.png)

![](https://global.discourse-cdn.com/choiceofgames/original/3X/1/c/1cadd055cdc85816d436c3846869a5472b28083c.png)

Your method looks like it could be more robust, though.

---

<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 8, 2016, 8:49pm UTC](https://forum.choiceofgames.com/t/using-rand-with-a-text-variable/19629/10 "2016-08-08T20:49:12Z")

</div>

After doing this for a while you learn some tricks to simplify long bits of code. :] Feel free to copy!
