0 votes
by (180 points)
edited by

Hello,

New IF writer here :)
I am trying to generate a passcode with digits and letters. Well... digits only for now.

I would like to store a series of random numbers into a string.
I don't want to use random(0,9999) as I will add letters in the string later.

I manage to display my random-generated passcode with

(random: 0,9)(random: 0,9)(random: 0,9)(random: 0,9)

but can I store it into a string ?

I tried :

(set: $TestCode to ((random: 0,9)+(random: 0,9)+(random: 0,9)+(random: 0,9))) 

which adds the numbers, and

(set: $TestCode to ((random: 0,9)+""+(random: 0,9)+""+(random: 0,9)+""+(random: 0,9)))

which causes a type mismatch error : the string "" isn't the same type of data as the number 9.

This question : http://twinery.org/questions/1606/twine2-harlowe-convert-integer-string-trying-random-encounters

brings an answer but it's quite cumbersome as every digit has to be stored in a variable :

(set: _First to (random: 0,9))
(set: _Second to (random: 0,9))
(set: _Third to (random: 0,9))
(set: $TempCode to (text:_First) + "A" + (text:_Second) + "B" + (text:_Third))
$TempCode

> 0A7B8 <- Success !


Is there an easier way ? Why can't I just use text:random(0,9) ?

Thanks !

Pip

1 Answer

0 votes
by (159k points)

The one of the responses to the question you linked to explained that the (text:) macro can be used to convert a number of different data types to String. Based on that fact your 2nd example could be re-written like so:

(set: $TestCode to (text: (random: 0,9)) + (text: (random: 0,9)) + (text: (random: 0,9)) + (text: (random: 0,9))) 

TestCode: $TestCode

 

Why can't I just use text:random(0,9)

The reason the above doesn't work is because the syntax of your calls to the (text:) macro and the (random:) macro are invalid, Harlowe's macro syntax is...

Open parenthesis + Macro name + Full colon + Parameters (if any) + Close parenthesis

... where the Parameters can be of any valid datatype including the result of macro call. So your example would of worked if you wrote it like so.

(text: (random: 0,9))

 

...