Java Can I assign probabilities to events in Java?

AI Thread Summary
To assign a probability to an event in Java, a random number generator can be utilized to simulate randomness. By generating a uniform random number between 0 and 1, you can create an if/else statement to determine whether a specific arithmetic operation should occur based on a defined probability. For example, if the generated number is less than 0.8, the operation will execute, reflecting an 80% chance of occurrence. This approach allows for the implementation of probabilistic behavior in code without needing a complex algorithm.
stonecoldgen
Messages
108
Reaction score
0
Hello.

So I'm wondering if there is a way to assign a probability to a certain event in the code.

Let's say that I want a certain arithmetic operation to have an 80% chance of happening (and make an if/else if statement based on that, or something similar...)

Is there something in the java language that can help me? or should I just create an algorithm to do it?

Thanks a lot.
 
Technology news on Phys.org
Hey stonecoldgen.

Although computers are engineered to always perform an instruction and get the right output pretty much 100% of the time, what you can do is use a good random number generator to simulate the random behaviour: it won't be purely random but it should be good enough.

So what you can actually do is have two options: the output of not doing something and the output of your arithmetic operation occurring. So in pseudo-code:

Code:
randomnumber = generate_uniform_random_number_between_0_and_1();

if (randomnnumber < 0.8)
   do_arithmetic_operation();
else
   dont_do_arithmetic_operation_or_something_else();
end if

You can implement other stuff the same kind of way.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top