Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  'Resumable' random number generation  (Read 1697 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Posted 2012-10-23 17:21:46 »

I need deterministic random number generation that I can save and restore at a later point. Annoyingly, java.util.Random doesn't allow you to get the current seed, only to set one. java.security.SecureRandom seems to let you get the current seed, so that's one alternative but I'm worried about the speed implications.

Anyone have any other alternatives? (Or, anyone used SecureRandom for this and found that it's fine?)

Thanks.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-10-23 17:46:55 »

Use reflection to read the private field holding the seed.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2012-10-23 17:57:10 »

From a design perspective, why would they make that field private and not provide a getter?  persecutioncomplex Are we not supposed to know seeds?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-10-23 18:02:59 »

I issue is that in Random, the 'seed' is supposed to be a conceptual seed, not a state.

In theory, the seed of a Random should be final, it will never change - like the seed of a tree never changes, it's just that the seed transforms into something completely different.

Whether that was the reasoning of the Sun developers... I don't know Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2012-10-23 18:08:05 »

I issue is that in Random, the 'seed' is supposed to be a conceptual seed, not a state.

In theory, the seed of a Random should be final, it will never change - like the seed of a tree never changes, it's just that the seed transforms into something completely different.

Whether that was the reasoning of the Sun developers... I don't know Smiley

I understand why it's final, but it seems natural to me to have the option to check what seed a particular generator is working with. I'd assume it's for security though, but that doesn't make sence if you can grab it by reflection anyway.

@Orangy: Why don't you provide the seed yourself, and save it before entering it into the Random object?

Offline Damocles
« Reply #5 - Posted 2012-10-23 18:30:51 »

Here is the source for Random:

http://developer.classpath.org/doc/java/util/Random-source.html

How about just making your own implementation without hasseling around with strange workarounds.

Just modify it and grab the seed anyway you want

Offline TimB

Junior Devvie


Medals: 1
Projects: 1



« Reply #6 - Posted 2012-10-23 18:38:44 »

Why not just set the seed when you need to "save" it?

If you need a midpoint snapshot you can always get a random from the current generator then set that as the seed...
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-10-23 19:25:15 »

It's a pretty common use case in pure functional code to "fork" a PRNG.  Now and again, even Java code takes a jaunt into functional land. Random is serializable, and the serialized form does include the seed.  This means you can deep-copy the RNG by serializing it, then use that copy to restore the state from whatever snapshot you took.  Would have been nice if it were also Cloneable but you can't have everything.

The same goes for ThreadLocalRandom, which you really ought to be using.  You can't use SecureRandom for this, because it truly is nondeterministic.

Also, recall that as long as you're not in an applet sandbox, you can always blow through access modifiers like private with reflection.  Mind the inherent thread-unsafety of this though (making ThreadLocalRandom that much more ideal)
Offline Best Username Ever

Junior Devvie





« Reply #8 - Posted 2012-10-23 20:58:29 »

Don't use reflection... Cranky
Ideally, just adapt existing code or write your own class. You can also override Random and change the next(int) method.
SecureRandom can behave deterministically or not depending on the implementation you choose.
You really should be able to get a copy of a Random object, and Random should have been abstract or an interface.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public class SomeRandomClass extends Random
{
  private long state;

  public SomeRandomClass(long seed)
  {
    state = seed;
  }

  protected int next(int bits)
  {
    state = (state * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
    return (int)(state >>> (48 - bits));
  }

  public long getSomeRandomClassState()
  {
    return state;
  }

  public void setState(long state) // Not the same as set seed
  {
    this.state = state;
  }
}
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2012-10-23 23:20:18 »

@Orangy: Why don't you provide the seed yourself, and save it before entering it into the Random object?

sproingie already touched on this, but basically I want to fork/save/reset a Random object. Something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
Random random = new Random(1234);

// use some random numbers
random.nextInt()
..

// Save the random state
long savedSeed = random.getSeed();

// use some more random numbers
int a = random.nextInt();
int b = random.nextInt();

// Rewind the random sequence
random = new Random(savedSeed);

// get the same sequence again
int c = random.nextInt(); // 'a' == 'c'
int d = random.nextInt(); // 'b' == 'd'


It's late now so I'll read this thread properly later, but I'm not keen on serialization or reflection tricks. I'll have a look into forking java.util.Random if I can't find any other open-source rngs.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #10 - Posted 2012-10-25 22:18:49 »

Just implement something like Xorshift or WELL512 and add get/set functions for the internal state.

I really like Xorshift. It's very fast and it passes most PRNG test suites (like diehard), too. Its internal state are just 4 ints.

弾幕 ☆ @mahonnaiseblog
Offline Roquen
« Reply #11 - Posted 2012-10-26 09:18:45 »

Well, xorshift state can be any number of 32 or 64 bit integers.  For 100-ulp(100) % of video game purposes any (well chosen set of constants) single integer state versions of either is already overkill.  But that's OK because they're fast.  Don't bother with anything "better" because it'll be over-engineering.  (As an aside I find that the WELL1024 is faster than WELL512)
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.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!