Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Hashing / Randomizing using (long, long) as input  (Read 1061 times)
0 Members and 1 Guest are viewing this topic.
Offline matheus23

JGO Kernel


Medals: 107
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
Offline moogie

JGO Knight


Medals: 12
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.
Online 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Roquen
« Reply #3 - Posted 2013-03-16 07:22:48 »

Opps..just relooked at your snippet.  Skip the XorShift part...just return Jenkins.
Online 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?
Offline 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:

Offline matheus23

JGO Kernel


Medals: 107
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! Smiley

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
Online 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.
Offline matheus23

JGO Kernel


Medals: 107
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? Smiley

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

JGO Knight


Medals: 27
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.

A bad function would be
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!