For some reason, using Math.random() * x will produce a random number at some points of the code, but will simply produce the max number at other points. Could there be something causing this that you don't need to see the code for?
Printable View
For some reason, using Math.random() * x will produce a random number at some points of the code, but will simply produce the max number at other points. Could there be something causing this that you don't need to see the code for?
Try giving it a seed. I think you can do it when creating a Random Object.
Code:Random(long seed)
Oh sorry. Youre using Math.random(). :blush:
Use the RandomNumberGenerator and ONLY use one instance of it. If you only use one instance it will hit every number only once and when every number has been hit, it restarts.
Think it's the Random class. I can't remember the name of it now.
System_Error can you please post a small sample code utilizing RandomNumberGenerator ?
Thank you!
karkas
The RandomNumberGenerator as in the way I put it is simply a Random object. The biggest thing you need to make sure of, is that only one instance is used:
That's the bad version. Notice there's an object created EACH time the getRandom method is called. That's not what you want. Look at a better version:Code:public class Test
{
public Test()
{
super();
}
public int getRandom()
{
Random r = new Random();
return r.nextInt();
}
}
Notice how the Random object is now global. This is so you don't get any 'patterns' in your code, but in reality, there's no way of escaping them. The bad part about the random object is it will NOT hit any number twice until all numbers have been 'hit' at least once. That's obviously not random since the odds of getting the same number twice is still there. A better way is the utilize random things on a computer such a clock. I've even heard some people using frequencies of sound and such.Code:private Random r;
public class Test
{
public Test()
{
super();
r = new Random();
}
public int getRandom()
{
return r.nextInt();
}
}
He posted the same question in VB.NET forum and there the "RandomNumberGenerator" is just like (java.security.SecureRandom) to Java. but what do I know ;)
System_Error and ComputerJy thank you for your replies. I'm interested in creating a routine that gives me a true random number (not using the pseudo-random generator e.g. rnd() in VB). I read somewhere that using
System.Security.Cryptography.RandomNumberGenerator will do the trick but I just don't know how to do that. I haven't decided on the language yet so Java is fine (personally I will like to use VB .net 2005).
Regards
karkas
There's no such thing as a true random number. If you want something close then use sound card or video card frequencies or something.
Well, there are hardware random number generators that use radioactive decay. Those are truly random :)
So are detectors for background radiation and similar natural whitenoise listeners.