This site requires JavaScript, please enable it in your browser!
Greenfoot back
Anthony_Tatowicz
Anthony_Tatowicz wrote ...

2012/3/2

Making better pseudorandom numbers?

1
2
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
As I have been programming in Greenfoot I have noticed that the pseudorandom numbers that Greenfoot.getRandomNumber() produces are not very random... This caused problems in certain cases for me. So i started using another method of generating random numbers that works very well and wanted to share it with everyone that has the same problem as I, so enjoy :).
import java.util.Random

//gets the current time in mili secs  to create a good seed for randomization
seed = System.getCurrentTimeMilis();
//creates a random number generator 
Random rand = new Random();
// sets the seed for creating more randomized numbers
rand.setSeed(seed);
//generates the random number (this generate different types of variables see docs to find all)
rand.nextInt();
Builderboy2005 Builderboy2005

2012/3/2

#
In what way are Greenfoot's random numbers not very random? I'm curious as to what is happening since I have been using Greenfoot random numbers for a while and havent noticed anything.
danpost danpost

2012/3/2

#
I just ran a test on the Greenfoot.getRandomNumber method and found no problems with it. In fact, I will upload the test scenario called 'Random Test'.
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
Yes it is hard to tell sometimes but if you look at the Greenfoot soruce code for the Greenfoot class you will see that they did not use a seed to generate the random numbers which makes them very predictable in a sense. they used something like this
private static final Random randomNumberGenerator = new Random();

public static int getRandomNumber(int limit) {
return randomNumberGenerator.nextInt(limit);
}
this also doesn't allow generation of any other types ex. doubles and also has limitations, for example
int random = Greenfoot.getRandomNumber(100);
since your storing that in random number in the random variable every time you call on that variable the random number will always be the same but rand.nextInt() in my example will not
danpost danpost

2012/3/2

#
In your example 'int random = Greenfoot.getRandomNumber(100);' you are setting that variable to the return of 'Greenfoot.getRandomNumber(100)'. Just call 'Greenfoot.getRandomNumber(100)' again for a new number (just like you call 'rand.nextInt()' for a new number. What is the diff?
danpost danpost

2012/3/2

#
If I said 'int random = rand.nextInt();', then random will hold the same number every time I call it also!
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
none, but you still are only able to generate ints and it is still much less random then without a seed and when generating random numbers you should always use a seed, in my opinion.
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
Im really just trying to show how to use java to make you own randomization if the built in greenfoot one is not cutting for your purposes..... Which i will create an example if i have time and post it tomorrow.
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
danpost wrote...
If I said 'int random = rand.nextInt();', then random will hold the same number every time I call it also!
;/ i didn't think that through but yes i know.
danpost danpost

2012/3/2

#
You can create a fractional part by generating a large int and then divide it, say by 1000 to add three decimal places. And what do you mean by 'much less random'?
danpost danpost

2012/3/2

#
As far as the seed is concerned, it just determines a starting point in generating the random numbers. It has nothing to do with 'how random' the returned numbers are.
Anthony_Tatowicz Anthony_Tatowicz

2012/3/2

#
I mean mathematically they aren't random hence the name pseudorandom, but we can greatly decrease the predictability by using a seed. Really it doesn't matter, but i'm a math nerd and think of things like probability law when it comes to generating random numbers.
davmac davmac

2012/3/2

#
Check the documentation for the Random class, particularly the "Random()" constructor: "Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor". The generator that Greenfoot uses is seeded, it's just that Greenfoot doesn't choose a specific seed. Your assertion that using the system time as a seed will result in numbers that are "more random" isn't correct!
Duta Duta

2012/3/2

#
danpost wrote...
I just ran a test on the Greenfoot.getRandomNumber method and found no problems with it. In fact, I will upload the test scenario called 'Random Test'.
I'm a bit late to the party, but I also made a scenario testing this and it worked perfectly on that aswell. Link.
darkmist255 darkmist255

2012/3/2

#
Duta's thing is a really good demonstration (I like seeing the math average itself out as more time goes on :D). Yeah, I used to think the getRandomNumber wasn't very random (this was when I really got into Doubles), but now I just generate a higher number (maybe 10x or 100x) and the divide by 10 or 100 (after casting as a Double, of course). With a bit more precision it looks 100% random, I notice no patterns, no "favourites" if you will.
There are more replies on the next page.
1
2