|
-
Jul 26th, 2005, 11:35 PM
#1
Thread Starter
Fanatic Member
Math.random() not being random?
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?
-
Jul 27th, 2005, 12:16 AM
#2
Dazed Member
Re: Math.random() not being random?
Try giving it a seed. I think you can do it when creating a Random Object.
-
Jul 27th, 2005, 12:20 AM
#3
Dazed Member
Re: Math.random() not being random?
Oh sorry. Youre using Math.random().
-
Jul 27th, 2005, 06:42 AM
#4
Frenzied Member
Re: Math.random() not being random?
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.
-
Jul 27th, 2005, 06:42 AM
#5
Frenzied Member
Re: Math.random() not being random?
Think it's the Random class. I can't remember the name of it now.
-
Jun 16th, 2006, 09:57 AM
#6
Member
Re: Math.random() not being random?
System_Error can you please post a small sample code utilizing RandomNumberGenerator ?
Thank you!
karkas
-
Jun 16th, 2006, 06:53 PM
#7
Frenzied Member
Re: Math.random() not being random?
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:
Code:
public class Test
{
public Test()
{
super();
}
public int getRandom()
{
Random r = new Random();
return r.nextInt();
}
}
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:
private Random r;
public class Test
{
public Test()
{
super();
r = new Random();
}
public int getRandom()
{
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.
-
Jun 16th, 2006, 08:38 PM
#8
Re: Math.random() not being random?
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 17th, 2006, 02:29 AM
#9
Member
Re: Math.random() not being random?
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
-
Jun 17th, 2006, 08:43 AM
#10
Frenzied Member
Re: Math.random() not being random?
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.
-
Jun 18th, 2006, 09:35 AM
#11
Re: Math.random() not being random?
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|