File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Implementing Probability in Java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Implementing Probability in Java ?" Watch "Implementing Probability in Java ?" New topic
Author

Implementing Probability in Java ?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hello,
Am trying to incorporate probablity in one of my Java applications. I know how to generate random numbers. But how does one set the probablity of a random number be genrated. I know it sounds kinda funny.. becasue the whole point of it being random is that the probablity of any nimber should be the same. But I've included some code. The code does the following :
It generates a random number between 0 and 10. And if the the number is less than 4 it perfroms a function.
If the number is more than or equal to 4 it does a function.
But how does one set the probablity of the number being generated. For eaxmple if the I wanted the probablity of 7 being genrated to be 0.4 and the probablity of a 3 being generated 0.1 and so on.
Does anyone out there have any ideas?
Here is the code. And thanks for reading.
yours respectfully Avin

import java.util.Random.*;

class AvinThread
{
public static void main( String argv[])
{
Start1 start1 = new Start1();
Start2 start2 = new Start2();

for(int i=0 ; i<100; i++)
{

int number =(int)(Math.random()*10);

if(number< 4)
{
start1.method1();
}

if(number >= 4)
{
start2.method2();
}
}

}
}

class Start1 extends Thread
{
public Start1()
{
}

public synchronized void method1()
{
System.out.println("HEY");

}

}
class Start2 extends Thread
{
public Start2()
{
}

public synchronized void method2()
{
System.out.println(" WOW ");

}

}


yours respectfully<br />Avin Sinanan
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
Hi,
I remember reading that the random number generator used is based on a PRBS ( Pseudo-Random Binary Sequence) so its not really random at all.
There are calls in java.util.Random such as r.nextInt( x) that will limit the selection to 0 - x(exclusive) for example, but I haven't seen anything like what you're looking for.

Bye,
Terry


Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
I've thrown this together quickly.
I think it works correctly. I'll test it later if I get a chance, or someone else might point out any mistakes. Also, someone else with a stronger mathematical background might have a better idea. But here is a basic framework:

Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
OK I've added some things. Following is the code for three classes. One is the applet. Another generates random numbers (now from 1 to 100, instead of just 1 to 10) adjusting to a special case probability. The third analyzes the generated numbers and displays a bar graph describing the numbers generated. I've also included the code for the html file to view it all. It's a bit much, but altogether it shows the distribution of the numbers generated.
The documentation is a bit cryptic and sparse, but it's something.




Hopefully that'll give you some ideas.
Good Luck,
-Dirk Schreckmann
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
As if that 300 lines of code weren't already way too much, I have something else to add.
I ran the program through 1000 iterations (that's 1000 times 100 special random numbers) and analyzed the distribution probability of the range of 20 to 29 (the third red bar from the left). It came out to about 55%, which would seem to be correct with my expectations. So that's the 50% probability for 20 as specified in the specialRandomNumber object, plus about 5% for the even distribution of the numbers from 21 to 29. I also changed the probability parameters, and each time the outcomes were as expected. So, it would seem to work correctly.
But, of course, it's been a few years since studying multivariable calculus, electrodynamics, etc. for me. So, I might very well be overlooking something of consequence.
OK, I promise to stop posting 300 lines of code (maybe).
Good Luck,
-Dirk Schreckmann
[ February 22, 2002: Message edited by: Dirk Schreckmann ]
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Dirk.. Thanks so much!!!
Hey even if it was a thousand lines I would appreciate it! the longer the better!!!
Thanks a million!
Can I ask you something.. you know anything about discrete event simulation?
Thanks!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
I did a quick search on google and found a few nice looking papers, essays, and articles on the subject.
Good Luck,
-Dirk Schreckmann
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Dirk, just as a bystander i have to say thank you as well, cause it helped me also.
great piece of code!
well done , and thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing Probability in Java ?