Announcement

Collapse
No announcement yet.

code ninja needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • code ninja needed

    My teacher is obsessed with magic 8 balls and she asked how many times it would take to roll every answer. I wrote a reeealy simple program and for some reason it wont work. I know there are other ways to write this using more complicated number checks but this way should be fastest since java's slow as crap. Anyways I know the conditional statement works even if it seems strange. What happens though is the same random number is picked 40 times in a row. Just so u know there are 20 sides to a magic 8ball. here's the source
    import java.util.Random;

    public class magic
    {

    public static void main(String args[])
    {
    int trials=50; //running the program multiple times to get a better average
    int results[]; //stores how many tries each trial took
    results=new int[trials];
    int average=0; //will become the average number of tries for all trials
    for(int a=0;a<trials;a++) //runs the loop trial number of times
    {
    int numgotten=0; //number of times an appropriate number was rolled
    int tries=0; //number of times the 8 ball has been rolled
    while(numgotten!=20) //rolls the 8ball until all 20 answers have been given
    {
    tries++; //records that the 8ball was rolled
    //System.out.println("numleft: "+(20-numgotten) + " " + " tries: "+tries); //debug helper
    if(roll(20-numgotten)) //requests the roll method to be called
    {
    numgotten++; //records that an unused answer was rolled
    //System.out.println("true"); //debug helper
    }
    else
    {
    //System.out.println("false");//debug helper
    }
    System.out.println(); //spacer
    results[a]=tries; //stores the number of tries that were needed to roll all 20 sides
    }
    }
    for(int b=0;b<trials;b++) //these three lines calculate the overall average of times the 8 ball was rolled
    average+=results[b];
    average/=(trials);

    for(int b=0;b<trials;b++)
    {
    System.out.println(results[b]); //displays all the results from the different trials
    for(int d=0;d<100000000;d++){} //the lazy programmer's delay
    }
    System.out.println("average of "+trials+" trials is "+average);
    }
    public static boolean roll(int a)
    {
    Random rand = new Random(); //problem is here or in next line
    int numpicked=rand.nextInt(20)+1; //range is 1-20
    //odds of getting new number is (20-numgotten)/20*100%
    //this sees if the number rolled is 1 the answers not yet rolled
    //you can imagine this as putting all the unpicked answers at the bottom and every time the answer is rolled
    //that answer is moved to the top and all the unused answers are moved down

    // System.out.println("random number:" +numpicked); //debug look close at this onw
    if(numpicked<=a)//need to see if random number is 1 we havent gotten
    return true;
    else
    return false;
    }
    }

    **edit: i added in some comments but i dpnt have a compiler with me so i dont know if this compiles. The problem is that for some reason the same random number is being sent over and over many times in a row. java can be at least up to 8 times slower than c/c++ depending on what ure doing. And what's wrong with my programming?? itll be easier to read if u
    Last edited by iKillburger; 11-02-2004, 06:50 PM.

  • #2
    CODE tags are nice.
    I fart out diamonds.
    sage

    Comment


    • #3
      CODE tags are nice............

      Code:
      import java.util.Random;
      
      public class magic
      {
      	
      	public static void main(String args[])
      	{
      		int trials=50;
      		int results[];
      		results=new int[trials];
      		int average=0;
      		for(int a=0;a<trials;a++)
      		{
      			int numgotten=0;
      			int tries=0;
      			while(numgotten!=20)
      			{
      				tries++;
      			//	System.out.println("numleft: "+(20-numgotten) + "  " + "  tries: "+tries);
      				if(roll(20-numgotten))
      				{
      					numgotten++;
      				//	System.out.println("true");
      				}
      				else 
      				{
      			//		System.out.println("false");
      				}	
      				System.out.println();
      				results[a]=tries;
      			}
      		}
      		for(int b=0;b<trials;b++)
      		average+=results[b];
      		average/=(trials);
      		
      		for(int b=0;b<trials;b++)
      		{
      		System.out.println(results[b]);
      		for(int d=0;d<100000000;d++){}
      		}
      		System.out.println("average of "+trials+" trials is "+average); 
      	}
      	public static boolean roll(int a)
      	{
      		Random rand = new Random();//problem is here or in next line
      		int numpicked=rand.nextInt(20)+1;//range is 1-20
      		//odds of getting new number is (20-numgotten)/20*100%
      		//see if 1 of 20 numbers is less than or equal to ones we havent gotten
      		
      	//	System.out.println("random number:" +numpicked);
      		if(numpicked<=a)//need to see if random number is 1 we havent gotten
      			return true;
      		else
      			return false;
      	}
      }

      Comment


      • #4
        Your code is seriously messed up.
        And Java is by no means "slow as crap".
        Last edited by Fallen Angel; 11-02-2004, 06:15 PM.
        There's no place like 127.0.0.1

        Comment


        • #5
          Code:
          import java.util.*;
          
          public class magic {
          	
          	static Random rand = new Random();
          	
          	public static void main(String args[]) {
          		boolean found[] = new boolean[20];
          		boolean all = false;
          		int count = 0;
          		while(!all) {
          			count++;
          			int thisRoll = roll();
          			
          			found[thisRoll] = true;
          			
          			if(!check(found))
          				all = true;
          		}
          		
          		System.out.println(count);
          	}
          	
          	public static int roll() {
          		return rand.nextInt(20);
          	}
          	
          	public static boolean check(boolean found[]) {
          		boolean aFalse = false;
          		for(int k = 0;k < found.length;k++) {
          			if(found[k] == false)
          				aFalse = true;
          		}
          		
          		return aFalse;
          	}
          }


          I'd probly do something more like the code I posted... it's a hell of alot easier than whatever it was you posted.
          Ban Ikrit

          Comment


          • #6
            well thanks 4 the help. still curious about why it errored like that tho.

            Comment

            Working...
            X