I'm looking for a small function from integers to integers - in a language that only has floats - that can act as a visual RNG. Normally I would use a function such as the one described here:

int cash(int x, int y){   
    int h = seed + x*374761393 + y*668265263; //all constants are prime
    h = (h^(h >> 13))*1274126177;
    return h^(h >> 16);

However, for the thing I'm working on, I have the added restriction of only using arithmetic operations. the operations I have available are: +, *, -, /, %, sign(), abs(), pow(base, exponent), ln(), floor(), as well as basic trig functions. I'm having difficulty finding anything that doesn't rely on having bitwise operations available. Does anyone know of a hash function that doesn't use xor, that produces reasonable visual randomness when fed a linear sequence of integers?

  • $\begingroup$ Which are you looking for? A hash function, or a pseudorandom number generator? The answer will probably be different. Note that you can similar integer arithmetic using floats, as long as the integers don't get too large (i.e., as long as the floats have enough precision), or by using big-int methods. You can implement xor by converting the integer to its binary representation and then xor-ing bitwise. There are some schemes that use array lookups and/or right/left shifts. The challenge with using floats is that the resulting scheme might not be deterministic/reproducible. $\endgroup$
    – D.W.
    Jul 30, 2017 at 23:02
  • $\begingroup$ I want a PRNG that takes a seed and no further information: aka, a hash function. I'm not using a language that has integers, or operations on them. xor just doesn't exist at all. I also can't do array lookups - it has to be 1. purely functional and 2. just arithmetic operations. the reason I want this is a bit silly, it's a code-golf type of thing. that's why I'm so limited, though - it really does have to only use the operations I mentioned. $\endgroup$
    – lahwran
    Jul 31, 2017 at 1:51
  • 3
    $\begingroup$ If you have booleans, you can build integer arithmetic (and xor) out of that. If you have floats, you can express booleans. It's all Turing-complete. I realize that doesn't answer your question exactly. $\endgroup$
    – D.W.
    Jul 31, 2017 at 6:09
  • $\begingroup$ yeah, I understand that, but that would take a lot of hacking - it's actually not Turing complete, I have to add more code to get more compute depth. I was hoping there would be a known function that only takes on the order of 30 operations - an implementation of binary operations would take far more. $\endgroup$
    – lahwran
    Jul 31, 2017 at 15:07

2 Answers 2


Found a solution here, as glsl, which at one time had near exactly the same restrictions I wanted:

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);

or as I implemented it: $\operatorname{mod}\left(\sin \left(a\cdot 12.9898+b\cdot 78.233\right)\cdot 43758.5453,\ 1\right)$


Low-discrepancy sequences might do what you want. Depending on what you want, they might be more useful than pseudo-random sequences. If you need more than one dimension, Halton sequences are popular.

This probably isn't going to help you in a code golf scenario, though.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.