inkle / ink

inkle's open source scripting language for writing interactive narrative.
http://www.inklestudios.com/ink
MIT License
4.13k stars 492 forks source link

Assigning a Random Number to Variables #168

Closed bdchaput closed 8 years ago

bdchaput commented 8 years ago

What I'm trying to do is add an option which adds a random amount to a variable. Basically:

Temp x = //random number intelligence = intelligence + x

I haven't yet figured out how to do this and the tutorial on the website doesn't seem to cover it (unless I've missed it). Is there a way to do this?

y-lohse commented 8 years ago

You could use shuffle to get a random number from a predefined set. But if you want a more random number, external functions are probably the way to go!

joethephish commented 8 years ago

Exactly as @y-lohse said, although since it's such a common request, I'm strongly considering adding as a standard feature/function in the engine.

bdchaput commented 8 years ago

How would I set a variable to a shuffled number. Right now I have:

TEMP x = 1|2|3|4|5

joethephish commented 8 years ago

Something like:

{random()}
{random()}
{random()}
{random()}

== function random ==
{shuffle:
 - ~ return 0
 - ~ return 1
 - ~ return 2
 - ~ return 3
 - ~ return 4
 - ~ return 5
 - ~ return 6
 - ~ return 7
 - ~ return 8
 - ~ return 9
}
joethephish commented 8 years ago

Commandeering this issue though, as a task to add:

RANDOM(min, max) - generates a random integer in those bounds. Inclusive or exclusive of those boundaries? It's up to the user to create their own derived functions (e.g. random float between 0 and 1) SEED_RANDOM(seed)

joningold commented 8 years ago

Inclusive of boundaries is probably most user friendly in the case of integers. Eg

~ temp dice_roll = RANDOM(1, 6)

Jon On Sat, 10 Sep 2016 at 2:23 pm, Joseph Humfrey notifications@github.com wrote:

Commandeering this issue though, as a task to add:

RANDOM(min, max) - generates a random integer in those bounds. Inclusive or exclusive of those boundaries? It's up to the user to create their own derived functions (e.g. random float between 0 and 1) SEED_RANDOM(seed)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/inkle/ink/issues/168#issuecomment-246111587, or mute the thread https://github.com/notifications/unsubscribe-auth/AA40oxDh_Nuf4i9t9o2l1f839Ofg9NZ3ks5qoq9dgaJpZM4J3daN .

y-lohse commented 8 years ago

Just a heads up: if seeding is allowed, users might expect the same outcome regardless of the platform (unity, web-browser, etc)? Which would in turn involve settling on a common PRNG.

joethephish commented 8 years ago

Yeees I was wondering about this. It's already a potential issue with the current shuffle behaviour. Relatedly I was wondering whether there are currently any differences in eg float formatting between C# and JS...

joethephish commented 8 years ago

RANDOM and SEED_RANDOM now implemented in the following commits:

y-lohse commented 8 years ago

For my own future reference, here's the C# PRNG : http://referencesource.microsoft.com/#mscorlib/system/random.cs,bb77e610694e64ca

Just glanced over it, it looks actually portable.

joethephish commented 8 years ago

Oh cool, not too much code there!

Wonder if it's worth boiling down the code so that we're not relying on Random within the C# code, to get parity between C#, JS and any other runtimes. I'm already abusing it slightly anyway.

y-lohse commented 8 years ago

Yeah I was expecting is to be based on Mersenne Twister with double-shifting paraglydes and a large coke or something like that, but no.

It might indeed be a good idea to "re-implement" it in ink. The PRNG for inkjs is the only notable addition to the codebase I had to make, and as you mentioned earlier this already leads to platform inconsistencies with shuffles.