Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2007, 9:12 AM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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
cwl157 is offline   Reply With Quote
Old Sep 28th, 2007, 10:35 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 28th, 2007, 2:58 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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.
cwl157 is offline   Reply With Quote
Old Sep 28th, 2007, 3:56 PM   #4
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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()
  {
       int[] ar = new int[100];
       Random r = new Random();
       int next = 0;
       int[] num = new int[6];
       int j = 0;
       
       for (int i = 0; i < 100; i++)
          ar[i] = i;
       
       
       for (int i = 0; i < 100; i++)
       {
            next = r.nextInt(100);
            if (ar[next] == -1)
                continue;
            else
            {
               if (ar[next] == 0 || ar[next] == 1 || ar[next] == 2 || ar[next] == 3 || ar[next] == 4 || ar[next] == 5)
               {
                   num[j] = ar[next];
                   ar[next] = -1;
                   j++;
               } // end if
            } // end else
            
       } // end for
       System.out.println();
       printArray(num);
       return true;
  }

EDIT: I noticed one error:

  else
            {
               if (ar[next] == 0 || ar[next] == 1 || ar[next] == 2 || ar[next] == 3 || ar[next] == 4 || ar[next] == 5)
               {
                   num[j] = ar[next];
                   ar[next] = -1;
                   j++;
               } // end if
            } // end else
should be:

 else
            {
               if (ar[next] == 0 || ar[next] == 1 || ar[next] == 2 || ar[next] == 3 || ar[next] == 4 || ar[next] == 5)
               {
                   num[j] = ar[next];
                   j++;
               } // end if
               ar[next] = -1;
            } // end else

Because 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.

Last edited by cwl157; Sep 28th, 2007 at 4:37 PM.
cwl157 is offline   Reply With Quote
Old Sep 28th, 2007, 5:09 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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()
  {
       int[] ar = new int[100];
       Random r = new Random();
       int next = 0;
       int[] num = new int[6];
       int j = 0;
       
       for (int i = 0; i < 100; i++)
          ar[i] = i;
       
       
       for (int i = 0; i < 100; i++)
       {
            next = r.nextInt(100);
            if (ar[next] == -1)
                continue;
            else
            {
               if (ar[next] == 0 || ar[next] == 1 || ar[next] == 2 || ar[next] == 3 || ar[next] == 4 || ar[next] == 5)
               {
                   num[j] = ar[next];
                   j++;
               } // end if
               //System.out.println(ar[next]);
               ar[next] = -1;
            } // end else 
       } // end for
       return num;
  }
cwl157 is offline   Reply With Quote
Old Sep 28th, 2007, 5:12 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Sep 28th, 2007, 5:18 PM   #7
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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?

Last edited by cwl157; Sep 28th, 2007 at 5:44 PM.
cwl157 is offline   Reply With Quote
Old Sep 28th, 2007, 5:54 PM   #8
mark
Newbie
 
Join Date: Mar 2006
Posts: 14
Rep Power: 0 mark is on a distinguished road
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?

Last edited by mark; Sep 28th, 2007 at 6:36 PM.
mark is offline   Reply With Quote
Old Sep 28th, 2007, 7:35 PM   #9
mark
Newbie
 
Join Date: Mar 2006
Posts: 14
Rep Power: 0 mark is on a distinguished road
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%

Last edited by mark; Sep 28th, 2007 at 7:46 PM.
mark is offline   Reply With Quote
Old Sep 28th, 2007, 5:53 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Perhaps the following will suffice for your purposes.

boolean doRand() {
    return (Math.random() < 0.06);
}
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
First Java Program duale2005 Java 3 May 22nd, 2006 5:17 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
Begin my first lesson to learn Java satimis Java 7 Mar 3rd, 2005 2:45 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC