Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Hashing / Randomizing using (long, long) as input  (Read 1301 times) 0 Members and 1 Guest are viewing this topic.
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Posted 2013-03-15 22:52:26 »

How can I pseudo-generate random numbers having a (long, long) as input?

I want the random number generator to generate the same numbers with the same seeds, of course, so I get the same random number when calling it with (1, 1) twice.

I've tried something like this:
 1  2  3  4  5  6  7 `long rand(long x, long y) {    final long a = 2147483647177471651L; // Didn't know any other big primes... should have been prime    final long b = 9223372036854775783L; // Biggest 63-bit prime    final long c = 1858404738118378471L; // Random number    final long d = 1592548884139547179L; // Random numbers    return ((a * x + b * y + c) ^ d);}`

but that gave me bad results when simply out-putting it to an image:

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
moogie

JGO Ninja

Medals: 15
Projects: 6
Exp: 10 years

Java games rock!

 « Reply #1 - Posted 2013-03-16 03:25:44 »

well the easiest way would be to use the standard Random class:

 1 `new Random(long1^long2).getLong();`

But if you do not want to use the standard Random Class then you can use a Multiply With Carry PRNG... http://en.wikipedia.org/wiki/Multiply-with-carry It even has some C code that you can just port to Java.

Java4k RIP 2014
Roquen
 « Reply #2 - Posted 2013-03-16 06:50:12 »

Don't try to write your own hash functions or random number generators.  I don't have time to throw together a code snippet ATM, but you can do the following:  break the 2 64-bit inputs into 4 32-bit, hash with mumurhash 2..this allows computing the initial 4 part in parallel which goes to a final cascade.  Then feed that to a 32 bit XorShift (there a code for that on this site).  Or if you need 64-bit output, use Jenkins to hash and feed to a 64-bit XorShift (also code on this site).

XORing two numbers is high correlated.
Roquen
 « Reply #3 - Posted 2013-03-16 07:22:48 »

Opps..just relooked at your snippet.  Skip the XorShift part...just return Jenkins.
Roquen
 « Reply #4 - Posted 2013-03-16 14:04:52 »

Sigh.  I guess I'm off my game today..what I had in mind was a hash by Wang:

 1  2  3  4  5  6  7  8  9  10  11  12 `public final static long hashWang(long key){  key = (~key) + (key << 21); // key = (key << 21) - key - 1;  key = key ^ (key >>> 24);  key = (key + (key << 3)) + (key << 8); // key * 265  key = key ^ (key >>> 14);  key = (key + (key << 2)) + (key << 4); // key * 21  key = key ^ (key >>> 28);  key = key + (key << 31);  return key;}`

This is probably much higher quality that you need, so it could probably replace with a cheaper function.  What's this to be used for?  Do you really want 64-bit integer result?
Cero
 « Reply #5 - Posted 2013-03-16 14:11:53 »

the only people who should write hashing/crypto algorithms are mathematicians.

use existing code D:

matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #6 - Posted 2013-03-16 16:39:18 »

Thank you very much, Roquen, this worked out very well!

I love simplex noise. Now also without pregenerating, but simply "asking" for a noise value at a given point (for learning, probably for using too, when performance is not needed):

https://github.com/matheus23/Utils/blob/master/Utils/src/org/matheusdev/noises/noise2/SimplexNoiseLayerLazy2.java

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Roquen
 « Reply #7 - Posted 2013-03-17 12:13:47 »

Food for thought. Note that you're doing massive overkill.  You're ultimately generating a float, so no need for a 64-bit hash.  The wang hash is excellent (by today's standards) and you only really need mediocre hash functions for noise functions (how good depends on which noise we're talking about). Also since you're only generating one random number per hash...just use the hash as the random number.
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #8 - Posted 2013-03-17 12:46:42 »

I don't know any hash functions...

What would be an example of a medicore hash function?

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #9 - Posted 2013-03-17 14:20:39 »

Depends on what you are using it for. For game noise and even hashing for hash tables, you want something fast and good enough. For simulation you want something fast, and good statistical properties. For crytography you want it to be as close as practical to a random oracle model and one way. Speed is a distant 3rd.

Rule one: for cryto get an official one. Never do your own.

Otherwise doing your own is quite practical but perhaps a little bit of a waste of time. For simulation my rng that i posted here, is many times faster than MT( a crap rng for simulations seriously) and has extra statistical properties over both MT and java.util.Random(), and faster than both. I said that already.

For games you can get away with a lot. A common one for 2 numbers is something like
 1 `result=(a*x+b)*y+c`
with primes a,b,c. This can be extended for any number of numbers. This will give some "patterns" but for the most part is not too bad. The * are not really that expensive compared to moving all the data around. Then you can just use bitshift mixing functions(aka the wang example), both conditional and non conditional shifts. There are lots around the web for that, with different values of good. Most are for hash table stuff but work non the less. There are some that are optimized for the old Shader langs as well. These often use small lookup tables combined with the above examples. Some use nested lookups
 1 `results=randTable[(randTable[x&0xff]+y)&0xff];`
. But i have never bothered since there is a small number space for that.

 1 `result=x+y;`
Since we get a linear relationship between both values, and close input results in close output. Typically we want it to be more random.

I have no special talents. I am only passionately curious.--Albert Einstein
Roquen
 « Reply #10 - Posted 2013-03-19 13:54:59 »

delt0r's suggesting is reasonable.  For my 2D simplex noise I use the downright abysmal:

 1 `  return ((y<<8)^(x)) * 0xB5262C85;`

This does have noticeable defects, but I've only seen them when zoomed out far enough that the result is white noise...so farther than is useful.  It's possible that I just haven't noticed them at lower scales however.

Whatever hash you use, you can do the following:

 1 `final float value00 = ((hash(cellX0, cellY0) >> 31) & 2) - 1;`

and if you're using a good to excellent hashing function, you can skip on the shift.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (28 views) 2015-04-16 10:48:47 Duke0200 (42 views) 2015-04-16 01:59:01 Fairy Tailz (32 views) 2015-04-14 20:13:12 Riven (33 views) 2015-04-12 21:36:37 bus hotdog (49 views) 2015-04-10 02:39:32 CopyableCougar4 (51 views) 2015-04-10 00:51:04 BurntPizza (51 views) 2015-04-06 22:06:58 ags1 (53 views) 2015-04-02 10:58:48 Riven (52 views) 2015-04-01 18:27:05 ags1 (69 views) 2015-03-31 10:55:12
 theagentd 27x BurntPizza 17x wessles 15x 65K 11x kingroka123 11x Rayvolution 11x alwex 11x KevinWorkman 9x kevglass 8x phu004 8x Hanksha 7x SHC 7x Olo 7x ra4king 7x Ecumene 7x Roquen 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org