Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  save random number sequence  (Read 2065 times)
0 Members and 1 Guest are viewing this topic.
Offline skinny boy

Junior Member





« Posted 2009-10-23 14:22:56 »

i couldnt find a good way to express the subject in the title so, if what you are about to read is not what you thought you would from the title, sorry!!!

read about the random number stuff, algorithms etc,,

what i didnt find anywhere is how to save the place the sequence has reached so far, so the code can continue from then on

the game asks the RandomNumberGenerator for a number
the first generated number is returned
for the second request the RNG returns the second number the specific algorithm generates
and so on

after the game stops,  i want it for the RNG to be able to know how many numbers it have generated so far, so as to continue the process of the "generation" from that place of the sequence

the stupid way is to save the number of numbers generated, and during the game initiallization ask for the RNG to create (and dumb) as many numbers as have been created....

do i make sense?

thank you

(my guess is that there are algorithms that allow you to pick the place of sequence you want it to start generating from and not from place 1 all the time.. but i cant seem to find any, if only  i were smarter i would create my own...)
Offline pjt33
« Reply #1 - Posted 2009-10-23 14:26:45 »

Take the java.util.Random source code, add a getter, rename the class, Bob's your uncle. Assuming you don't mind the licensing issues this throws up.
Offline Roquen
« Reply #2 - Posted 2009-10-23 14:55:21 »

Generators each have a state. You simply need to save and restore this state information to continue the sequence.

Check out this thread:
http://www.java-gaming.org/topics/math-random/18426/view.html

O.K.  I'll bite:  Why do you want to do this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #3 - Posted 2009-10-23 15:18:14 »

if only  i were smarter i would create my own...)
You could use pseudorandom;
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
       long randomSeed=0;// or whatever seed you like

   public float rnd()
   {
      int bits=16;

      // generate random number between 0-65535
     randomSeed = (randomSeed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
      float r=(float)(randomSeed>>>(48-bits));

      // return 0-0.99f
     return (r/65536);
   }

This code will always generate the same sequence of 'random' numbers from a given seed. With this you can go back to any previous states by replacing the current seed with one saved from an earlier state.

People make games and games make people
Offline skinny boy

Junior Member





« Reply #4 - Posted 2009-10-23 22:08:04 »

Quote
Generators each have a state. You simply need to save and restore this state information to continue the sequence.
 
dont really get it, the only info tha a generator holds is the seed
(of course i am wrong, but thats what i have managed to grasp so far)

Quote
thanks for the link (didnt really get much, but thanks anyway)

Quote
O.K.  I'll bite:  Why do you want to do this?
in my (miserable) attempt to creating a Dungeon Master assistant software
i have noticed that people use their laptops and notebooks and netbooks and iPhones and..
and ofcourse they are really spoiling the fun but since they do it, why shouldnt i write a software?

the main idea is this: all (pseydo) random number generator algorithms try to fulfill some statistical criteria that apply to (trully) random numbers. but, as the Law of Large Numbers dictates, the sample (the random numbers used in the game) must be larger than countable in order for the hypothesis to be true (randomness). since the game of DnD uses a lot of dice , i believe that it can improve on the illusion of random numbers , in comparison to just changing the seed and using the same math from the beginning. for example having a day of luck and another of bad luck instead of having an averge day everytime


Quote
This code will always generate the same sequence of 'random' numbers from a given seed. With this you can go back to any previous states by replacing the current seed with one saved from an earlier state.
if only i were smarter i would be able to trully understand this piece of code
it is what i am looking for, i think
and 65.000 are more than enough random numbers

thank you both

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #5 - Posted 2009-10-23 23:39:42 »

For D&D you really don't need anything except pseudo randomness. Just initialize your seed every time you start up with the current time in milliseconds or nanoseconds. This more than suffices to keep every session different and seemingly balanced in terms of range.

The state of a random number generator is the seed. A generator typically doesn't store anything else.

See my work:
OTC Software
Offline SimonH
« Reply #6 - Posted 2009-10-24 01:27:40 »

if only i were smarter i would be able to trully understand this piece of code
Lol! Me too! Don't worry about the maths, just think of it as a 'black box' that given a start number will always give you the same sequence of random numbers.
NB the code I posted returns a float between 0 and 0.99 (like Math.random()) not as you suggested 0 and 65...

People make games and games make people
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2009-10-24 08:36:57 »

You might find this simpler:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
static private int randomSeed = (int)System.currentTimeMillis();

/**
 * Returns a random number between 0 (inclusive) and the specified value (inclusive).
 * @param range Must be >= 0.
 */

static public final int random (int range) {
   int seed = randomSeed * 1103515245 + 12345;
   randomSeed = seed;
   return ((seed >>> 15) * (range + 1)) >>> 17;
}

Props go to Riven for this code! The value of randomSeed determines the next random number. So at any time you can store the value of randomSeed and continue or replay the random numbers generated.

I have a feeling, as Demonpants mentioned, that you don't actually have a need to store the seed. Just initialize with System.currentTimeMillis() and you'll get random numbers every time.

For similar code to generate other types (eg floats):
http://www.java-gaming.org/topics/fast-math/21135/view.html

Offline skinny boy

Junior Member





« Reply #8 - Posted 2009-10-24 16:51:07 »

thank you for the code (and pass my thanks to Riven)

being able to continue the sequence of random numbers (even if its not needed for DnD) strikes me as beautiful, so i want to do it.
thats why i tried, i couldnt, i asked and i learned

(maybe i or somenone else will find a good reason of using it apart from DnD)
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #9 - Posted 2009-10-24 19:15:50 »

Simon's code is essentially exactly the same as Riven's code...

There was a thread somewhere with a bunch of these posted. I put one up too. It's all the same idea, though. Basically you put this hard to understand bit shifting into your random function and then never look at it again. It works fine.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #10 - Posted 2009-10-25 06:45:30 »

The shifts make a fixed point multiply (they add up to 32).  It's broken in two parts to allow space for the result of the multiply.  The plus one is to deal with truncation.
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.

ctomni231 (39 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (29 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (38 views)
2014-07-14 16:23:47

Riven (37 views)
2014-07-14 11:51:35

quew8 (33 views)
2014-07-13 13:57:52

SHC (70 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!