Login
Remember
IF Answers
Ask a Question

# How to add a random number generator in Squiffy?

0 votes
154 views
asked Mar 23 in Authoring by Natas (17 points)
recategorized Mar 27 by Alex
Hello there. I am Dandre and I am trying to create a text based game called Token Ino with Squiffy. The current problems I have been having has been using javascript to create a random number with the following psudeo code.
var randomnumber = function rng(min, max) {
return Math.random() * (max - min) + min;
}
I am not sure if I am doing something wrong because if I try to call {randomnumber} or reference it in a if then statement it doesnt show a number or the vaule is equal with null. I simply want a variable that is randomly generatored. :) Also if it isnt too hard to implent is it possible for me to have chance in Squiffy implented in any kind of way using javascript? Imagine if you had a var 1 = 2 and with var 1 being to that increases your chances of succuding by 60 percent. This could also be used in the 'combat' system I am working on.

- squiffy

## 1 Answer

+1 vote
answered Mar 23 by Alex (486 points)
selected Mar 23 by Natas
Best answer

For this to work you need to set an attribute to the random number. To do this, call the "set" function to store the result of your randomnumber function.

For example:

        var randomnumber = function rng(min, max) {
          return Math.floor(Math.random() * (max - min) + min);
        }

        set ("mynumber", randomnumber(1,6));

    Generated {mynumber}.

Live example: http://docs.textadventures.co.uk/squiffy/scratchpad/?gistid=74554bd2bb812a82df46&filename=gistfile1.txt

## Related questions

- How to compare two variables in Squiffy?
- How to deal with large numbers in Squiffy?
- Can I add dud links and tooltips in Squiffy with Javascript?
- Random text in Squiffy
- Why does this Javascript not work in squiffy but will in a js editor

...