Author Topic: State-based RNG (linear-congruential)  (Read 3205 times)

A (very) simple random number generator written in plain TorqueScript which allows you to create however many private, separate-state generators as you need.

When it comes to randomness? It's random "enough" for anything you'll need to do in Blockland. Just don't use it for bank-level cryptography and you're fine.

Usage

To create a new RNG state, call createRandContext([seed]). This will return a new "generator handle" which you can pass to all the other functions to use it. The generator will, by default, use the current platform time as the initial seed unless you explicitly pass an alternative using the seed argument.

%context = createRandContext();
echo(rand(%context)); // 0.150304, for example
echo(rand(%context, 5)); // 3.11251, for example
echo(rand(%context, 100, 200)); // 138.01, for example


Get/set the seed using getRandSeed and setRandSeed. For example:

setRandSeed(%context, 64);
echo(rand(%context, -10, 10, 0)); // -6
echo(rand(%context, -10, 10, 0)); // 4
echo(rand(%context, -10, 10, 0)); // -1
echo(rand(%context, -10, 10, 0)); // 0
echo(rand(%context, -10, 10, 0)); // 6

// set the generator back to it's initial value
setRandSeed(%context, 64);

// exact same sequence
echo(rand(%context, -10, 10, 0)); // -6
echo(rand(%context, -10, 10, 0)); // 4
echo(rand(%context, -10, 10, 0)); // -1
echo(rand(%context, -10, 10, 0)); // 0
echo(rand(%context, -10, 10, 0)); // 6


Function Reference

createRandContext([seed])

Returns a numeric "generator handle" which can be passed to other functions.
If seed is omitted, it defaults to getRealTime().

Note: This does not create an object. You don't need to clean up by deleting anything.

rand(context, [min], [max], [precision])

Returns a random number generated using the given context. If given, precision will round the result before returning. Use 0 if you want integer values. This function has the following signatures:

 rand(context)
    Returns a random floating point number from 0 to 1, inclusive.
  rand(context, x)
    Returns a random floating point number from 0 to x, inclusive.
  rand(context, a, b)
    Returns a random floating point number from a to b, inclusive.
  rand(context, a, b, c)
    Returns a random integer or floating point number from a to b, inclusive, rounded to precision digits.


getRandSeed(context)

Returns the initial seed value of the given generator context.

setRandSeed(context, seed)

Sets the initial seed value of the given generator context to seed and sets its state back to it.

Source

https://gist.github.com/portify/7403514 (37 LOC)
« Last Edit: November 10, 2013, 03:58:18 PM by Port »

cool

even though i don't get it.. :[

torquestuff isn't even fast enough for any kind of useful crypto that can't be broken by an outside program in seconds (and it takes TS minutes to process it!)
edit: wait no that's a stuffty over generalization that is totally unrelated to the topic at hand. scratch that.

i challenge someone to make a working, decent implementation of, say, a modified aes (that works with ascii characters, instead of binary bits), or even rsa if possible, that doesn't kill the game during processing. elliptic curves would work too, i hear they're lightweight

another idea: someone make arbitrary precision arithmetic that runs on a schedule loop or something so it doesn't kill the game
« Last Edit: November 10, 2013, 03:36:39 PM by Lugnut »

torquestuff isn't even fast enough for any kind of useful crypto that can't be broken by an outside program in seconds (and it takes TS minutes to process it!)

It's random "enough" for anything you'll need to do in Blockland. Just don't use it for bank-level cryptography and you're fine.

Randomness is not exclusive to cryptography.

Updated rand.cs to simplify a lot of things - went from 74 lines to 37 lines.

torquestuff isn't even fast enough for any kind of useful crypto that can't be broken by an outside program in seconds (and it takes TS minutes to process it!)
I made a text encryptor that has ~2^60 security in torquescript. It takes a couple seconds to load because of the massive substitution boxes but besides that it's actually pretty darn fast.

But yes, that's incredibly off topic.

Is there any way to say, implement a Mersenne Twister in TS?

Randomness is not exclusive to cryptography.
that's why i edited my post

torquestuff isn't even fast enough for any kind of useful crypto that can't be broken by an outside program in seconds (and it takes TS minutes to process it!)
edit: wait no that's a stuffty over generalization that is totally unrelated to the topic at hand. scratch that.

i challenge someone to make a working, decent implementation of, say, a modified aes (that works with ascii characters, instead of binary bits), or even rsa if possible, that doesn't kill the game during processing. elliptic curves would work too, i hear they're lightweight

another idea: someone make arbitrary precision arithmetic that runs on a schedule loop or something so it doesn't kill the game

If you need to 1-way encrypt data, there is a default sha1() function..

If you need to 1-way encrypt...

Encryption almost definitionally has a matching method for decryption. Yes, you can use lookup tables, but salting breaks that and EVERYONE can use it.

Hence why I said "1-way encrypt?" I don't understand what you're saying.

Hence why I said "1-way encrypt?" I don't understand what you're saying.

"Odorless smell"

Encryption is just the obfuscation of data in a way that it can later be referenced. There's two types of it, one way and two way encryption. One way encryption permanently obfuscates data, but the same result can be gotten with the same input. Two way encryption is also called a cypher and can be decrypted using the inverse of the encryption algorithm.

trinick, if i wanted a hash function i would've challenged people to make a hash function
both the things i suggested (aes and rsa) are "two way" encryption schemes

trinick, if i wanted a hash function i would've challenged people to make a hash function
both the things i suggested (aes and rsa) are "two way" encryption schemes
There's no real good way to implement AES or RSA in torquescript. (especially RSA since it deals with unreasonably large numbers)

On top of that there's no real way to change it to work for ascii characters unless you want to just use base64 or something
« Last Edit: November 10, 2013, 11:15:58 PM by Ipquarx »

On top of that there's no real way to change it to work for ascii characters unless you want to just use base64 or something
good point, i hadn't really put much thought into that idea

arbitrary precision arithmetic isn't inherently hard though, right? it's basically just implementing normal addition and subtraction algorithms manually, right?
(for 50+5, add 5 to 0, then add 5 to 0, dropping them down as you go so you get 5 and 55)
Code: [Select]
.50   .50
+.5  +.5
--- -> ---
..5  ...55
(periods added in for spacing)
« Last Edit: November 10, 2013, 11:25:40 PM by Lugnut »