Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Random seed question...  (Read 1842 times) 0 Members and 1 Guest are viewing this topic.
gbeebe

Senior Devvie

Medals: 5
Projects: 1

 « Posted 2011-09-28 03:09:38 »

The game I'm working on has a random 2d level generator.  The subrutine, that creates the level, accepts a (long) argument that if not zero, uses that value as the random seed.  I have some numbers (seeds) that create some interesting levels.  So when I use the number zero, it doesn't set the seed, and just leaves the "default" seed alone.  I have seen some pretty interesting levels generated this way as well.  My question is:  Is there a way to get the current seed that the random generator is using (when I have not specifically set one)?  I believe if I could get this number then I could virtually use this as the level ID number and allow players to use the same "random" level over and over.
gbeebe

Senior Devvie

Medals: 5
Projects: 1

 « Reply #1 - Posted 2011-09-28 03:59:21 »

Solution:
After giving it some thought, I came up with a work-around.

 1  2  3  4  5  6  7  8  9 `Random rndm = new Random();              if (seed != 0)       {          rndm.setSeed(seed);       } else {          seed = rndm.nextLong();          rndm.setSeed(seed);       }`

If no seed (not zero) is provided, I just randomly randomize the seed.
The subrutine is now a function that returns the seed, so I can store it in a value, so the players can request the "ID" of the level.
oNyx

JGO Coder

Medals: 2

pixels! :x

 « Reply #2 - Posted 2011-09-28 09:07:51 »

Replace:

seed = rndm.nextLong();

with:

seed = System.currentTimeMillis();

If you want the default behavior.

theagentd

« JGO Bitwise Duke »

Medals: 454
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2011-09-28 10:30:31 »

oNyx's one is the "default" one used by Random, but Gbeebe's one is "better". With the default one, you're losing 40 years worth of maps! xD

Myomyomyo.
Cero
 « Reply #4 - Posted 2011-09-28 10:40:44 »

oNyx's one is the "default" one used by Random, but Gbeebe's one is "better". With the default one, you're losing 40 years worth of maps! xD

lol =D

btw if you just don't set a seed, isn't it default behavior that it uses none/random
not sure about the Random class, might confuse it with some C++ class

theagentd

« JGO Bitwise Duke »

Medals: 454
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2011-09-28 12:43:17 »

Minecraft requires a seed for the world generator. It uses System.currentTimeMillis() if I'm not mistaken.
From http://www.minecraftwiki.net/wiki/Seed_(Level_Generation):
Quote
If you leave the seed field blank, the game will use the time (computer clock) as a seed. Leaving the seed field blank will not produce the same map every time. Using seed 0 in singleplayer will also be ignored, as though it were left blank.

Myomyomyo.
Cero
 « Reply #6 - Posted 2011-09-28 13:45:23 »

Minecraft requires a seed for the world generator. It uses System.currentTimeMillis() if I'm not mistaken.
From http://www.minecraftwiki.net/wiki/Seed_(Level_Generation):
Quote
If you leave the seed field blank, the game will use the time (computer clock) as a seed. Leaving the seed field blank will not produce the same map every time. Using seed 0 in singleplayer will also be ignored, as though it were left blank.

I just thought that if you dont even call .setSeed(seed);, using System.currentTimeMillis() or something similar is the default behavior.

theagentd

« JGO Bitwise Duke »

Medals: 454
Projects: 2
Exp: 8 years

 « Reply #7 - Posted 2011-09-28 15:26:50 »

 1  2  3  4  5  6  7 `public Random() {    this(seedUniquifier() ^ System.nanoTime());}public Random(long seed) {    this.seed = new AtomicLong(initialScramble(seed));}`

It also uses some fancy functions to make the seed "more random". Doesn't work that well IMHO. Try feeding it the values 0-100 and generating a random value, and you'll see what I mean...

Myomyomyo.
Pages: [1]
 ignore  |  Print

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

 Ralphanese (21 views) 2015-05-29 13:07:55 Gibbo3771 (17 views) 2015-05-29 06:54:17 Ralphanese (23 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (38 views) 2015-05-27 22:21:08 CopyableCougar4 (27 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (33 views) 2015-05-23 20:23:34 Waterwolf (39 views) 2015-05-20 15:01:45 chrislo27 (46 views) 2015-05-20 03:42:21
 Riven 27x Rayvolution 25x theagentd 22x BurntPizza 20x Spasi 17x KevinWorkman 16x orangepascal 16x Drenius 15x ra4king 13x opiop65 12x DavidBVal 12x princec 12x Husk 11x EgonOlsen 10x kevglass 9x Slyth2727 8x
 Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How 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:02
 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