HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Random Numbers in a Deterministic Way

#############################

Problem

You want to generate random numbers in a deterministic way, i.e. for each execution of the program, you want to get the same set of random numbers.

I needed such a solution in a program in which I needed to pick some random elements from a matrix, but I also needed a guarantee that repeated execution of the program will produce exactly the same output.

Solution

import java.util.Random;

Random rand = new Random(20090925);   // trick: specify a seed

for (int i = 0; i < 10; ++i)
{
   int pick = rand.nextInt(10);
   System.out.print(pick + " ");
}
System.out.println();

It will produce the set "0 5 4 4 3 2 3 4 6 8" each time you execute this code.

More info: http://java.about.com/od/javautil/a/randomnumbers.htm .

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2009 September 25, 22:03