![]() |
figuring out chance in java
Hi,
I have a problem that there is a 6% chance of it happening. I need a method that will return true if its supposed to happen or not. So basically, I need a method that will return true 6% of the time. i thought about random numbers but this will not return true 6% of the time everytime. Anyone have a better solution then just picking random numbers? I also need a method that will return true 90% of the time. I assume that these 2 will be similar but im at a block when it comes to figuring out how to make a method that will do this. Thanks |
Frankly, that sounds a tad flakey. If it's really dealing with basic probability, then it would be a random function.
You can think of it this way, though: Suppose I put 94 white and 6 black beans in a bag and 100 people have to each draw a bean. The guys that draw the black beans get shot. That's exactly 6 out of 100 if you don't put the beans back. Work it that way. Think array. Pick an element at random. Mark it so if it's chosen again, a new number is picked. Same thing as dealing cards. |
i don't really follow what your saying. So create an array of 100 elements and then randomly select six and then return true if what? How is that any different from using java's built in Random class and saying nextInt(100) 6 times, where the total your choosing from is 100, since i assume i will be using this to pick them from the array anyway.
|
ok, so i kind of get it now, because when i just say pick a random number 1- 100 or 0-99 or whatever it will put that number back to be picked again, which is why it can be more then 6% because it can pick those numbers twice. But if i take the number out once its picked, say replace it with a -1 in the array then it cant be picked again, so 6 out of every 100 times, it should come up with:
0,1,2,3,4,5 being picked out of an array going 0-99 and that would make it a true 6%. Well here is the method i got for it, i tested it once but dont really know if it worked. Tell me if you see anything wrong with it. Just ignore the return true for now, that comes after i select the right numbers, telling if it should return true or false. Also, i need one that is true 90% of the time as well. :
static boolean randomNumber1()EDIT: I noticed one error: :
else:
elseBecause no matter what value is selected, even if its a white bean, it still needs to be switched to -1 so its not selected again. So, now i just need to figure out how to extract the 6 black beans from whats selected and see if it is equal because the black beans will only be there 6 out of 100 times, i think. |
ok One more post on it, then ill wait for some feedback, but just working on it here i got this. I run it 100 times and it looks to be consistently giving back 7 out of every 100 return an array of the numbers 0,1,2,3,4,5 in any order. So that would mean that it give back the black marbles 7 percent of the time and not six. anyone see what i'm missing? Thanks.
:
static int[] randomNumber1() |
Choosing a random array index until you find one that hasn't already been selected is very inefficient; by the time there is only one valid index left, there will be only a 1% chance of choosing that index on each attempt. It may require many more than 99 attempts to select a valid index--and that's just for the 100th number!
One way to avoid this problem is to populate an array with valid numbers, shuffle the array, create a linked list from the array, and pop a number (node) off the top each time the method is called. |
wow, first, how do i "shuffle the array" and then i know java has a linked list class i could use. This might be getting more complicated then i wanted. This is really a small part of the overall problem I have to solve and i only have the weekend left to finish the whole thing. That seems like something that if i had more time i would do. When i first thought of probability i was ready to be set with the random numbers and just having the average be 6% but i knew there had to be something better out there and i also need to calculate the same thing but 90%. I'm pretty sure i know the course of the rest of the program after i figure out this stuff, i just didn't think it would be that hard to tell java to return true 6% of the time or 90% of the time but i guess i thought i was wrong. I will look into though, i suppose. I mean i get the whole selection without replacement its just what sort of loop would i need for it to work like i want it to? And then after i shuffle and pop what am i looking for, what makes the method return true if the first 6 pops are 1 - 6 or each time the method is called if the number is 1 or 2 or 3 or 4 or 5 or 6 then its considered true?
|
Perhaps the following will suffice for your purposes.
:
boolean doRand() { |
you need to understand basic probality
specify conditional probality here is a link http://www.cs.gmu.edu/cne/modules/da...cprob_bdy.html the probality of pick either 0,1,2,3,4,5 out of 100 is not 6% here's a hint the probabiltiy of x + probabiltiy of not x = 1 x = the probability of the function returning true is this true in your case? |
whoops i missread your post, i thought you said that you want a function to return true 6% of the time and return 90% of the time false
i made a mistake the probabilty of picking either 0,1,2,3,4,5 out of 100 is 6% |
| All times are GMT -5. The time now is 3:10 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC