programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Implementing Probability in Java ?

Ranch Hand
Posts: 109
• Number of slices to send:
Optional 'thank-you' note:
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.*;

{
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();
}
}

}
}

{
public Start1()
{
}

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

}

}
{
public Start2()
{
}

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

}

}

Ranch Hand
Posts: 328
• Number of slices to send:
Optional 'thank-you' note:
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

Sheriff
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
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

Dirk Schreckmann
Sheriff
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 109
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 732
• Number of slices to send:
Optional 'thank-you' note:
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.

 Don't get me started about those stupid light bulbs.