help me to check this RandomNumber

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    help me to check this RandomNumber

    I need help on my random number program.
    It is supposed to generate 40 random numbers (large numbers). But I just get 10 numbers and the result text file is emptied ...please teach me.
    Thanks
    Code:
    import java.util.Random;
    import java.io.*;
    
    class RandomNumber 
    {
    
      public static void main (String args[]) 
      {  
        int[] ndigits = new int[40];
        double x;
        int n;
        
       Random myRandom = new Random();
      
        for (int i = 0; i < 40; i++) 
        {
         	 ndigits[i] = 0;
       	} 
          	for (long i=1000000; i < 100000000; i++) 
        	{
                	 x = myRandom.nextDouble() * 10.0;
         	 n = (int) x;
         	 //count the digits in the random number
         	ndigits[n]++;
       		}
       		
       		try 
       		{ 			
        	FileWriter writer = new FileWriter("randomnumber.txt");
        	PrintWriter outFile = new PrintWriter(writer); 
        	    		  
      	  		for (int i = 0; i < 40; i++) 
      	  		{
         			      			 
         			System.out.println(i+": " + ndigits[i]);
         			outFile.println(i+": " + ndigits[i]);
         			 
          		}
          		      		
        	}
        	catch (IOException exc)
        	{
        		System.out.println("Can't write into this file");
        	} 
      }
      
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    I need help on my random number program.
    It is supposed to generate 40 random numbers (large numbers). But I just get 10 numbers and the result text file is emptied ...please teach me.
    Thanks
    Code:
    import java.util.Random;
    import java.io.*;
     
    class RandomNumber 
    {
     
    public static void main (String args[]) 
    { 
    int[] ndigits = new int[40];
    double x;
    int n;
     
    Random myRandom = new Random();
     
    for (int i = 0; i < 40; i++) 
    {
    	 ndigits[i] = 0;
    	} 
    	for (long i=1000000; i < 100000000; i++) 
    	{
    	 x = myRandom.nextDouble() * 10.0;
    	 n = (int) x;
    	 //count the digits in the random number
    	ndigits[n]++;
    		}
     
    		try 
    		{ 			
    	FileWriter writer = new FileWriter("randomnumber.txt");
    	PrintWriter outFile = new PrintWriter(writer); 
     
    			 for (int i = 0; i < 40; i++) 
    			 {
     
    			System.out.println(i+": " + ndigits[i]);
    			outFile.println(i+": " + ndigits[i]);
     
    		}
     
    	}
    	catch (IOException exc)
    	{
    		System.out.println("Can't write into this file");
    	} 
    }
     
    }
    How large are the numbers supposed to be?
    To open file in append mode You use new FileWriter("fil eName", true);

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by r035198x
      How large are the numbers supposed to be?
      To open file in append mode You use new FileWriter("fil eName", true);
      Numbers between 1,000,000 - 100,000,000
      Able for me to get those numbers?

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #4
        This is the result :

        0: 9896506
        1: 9895847
        2: 9897647
        3: 9899801
        4: 9904495
        5: 9902275
        6: 9900764
        7: 9900471
        8: 9901033
        9: 9901161
        10: 0
        11: 0
        12: 0
        13: 0
        14: 0
        15: 0
        16: 0
        17: 0
        18: 0
        19: 0
        20: 0
        21: 0
        22: 0
        23: 0
        24: 0
        25: 0
        26: 0
        27: 0
        28: 0
        29: 0
        30: 0
        31: 0
        32: 0
        33: 0
        34: 0
        35: 0
        36: 0
        37: 0
        38: 0
        39: 0

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by shana07
          Numbers between 1,000,000 - 100,000,000
          Able for me to get those numbers?
          Yes you can use

          Code:
           
          final int million = 1000000;
          final int hundredMillion = 100000000;
          int[] ndigits = new int[40];
          for (int i = 0; i < 40; i++) {
           int num = (int)(Math.random() * hundredMillion);
           if(million < 1000000) {
            num = num + million;
           }
           ndigits[i] = num;
          }
          Do you want the numbers to be unique?

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #6
            Originally posted by r035198x
            Yes you can use

            Code:
             
            final int million = 1000000;
            final int hundredMillion = 100000000;
            int[] ndigits = new int[40];
            for (int i = 0; i < 40; i++) {
             int num = (int)(Math.random() * hundredMillion);
             if(million < 1000000) {
              num = num + million;
             }
             ndigits[i] = num;
            }
            Do you want the numbers to be unique?
            Yeah it works. TQ.
            So we have to multiply the random numbers with max number to get between those range...
            You mean the number won't be the same?
            Yes please teach me that too...

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by shana07
              Yeah it works. TQ.
              So we have to multiply the random numbers with max number to get between those range...
              You mean the number won't be the same?
              Yes please teach me that too...
              What I meant to ask was, do you want each of the 40 random numbers to be different from each other (as in no duplicates).

              Comment

              • shana07
                Contributor
                • Jan 2007
                • 280

                #8
                Originally posted by r035198x
                What I meant to ask was, do you want each of the 40 random numbers to be different from each other (as in no duplicates).
                Yes, please teach me :)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by shana07
                  Yes, please teach me :)
                  What do you think can be done to achieve that?

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #10
                    Originally posted by r035198x
                    What do you think can be done to achieve that?
                    We have to compare between a & b
                    if a == b
                    then generate again random numbers? ;)

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by shana07
                      We have to compare between a & b
                      if a == b
                      then generate again random numbers? ;)
                      Which a and b? Remember we are storing the ones we have already generated in the array called ndigits.

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #12
                        Originally posted by r035198x
                        Which a and b? Remember we are storing the ones we have already generated in the array called ndigits.
                        I thought I can suprise you by doing this simple logic myself first. But I need to refer to you still...
                        Okay what I can think is something like:
                        ex:
                        if (ndigits[1] == ndigits[0])
                        {
                        num=regenerate random number;
                        ndigits [1];
                        }
                        How is that?

                        Comment

                        • shana07
                          Contributor
                          • Jan 2007
                          • 280

                          #13
                          Originally posted by shana07
                          I thought I can suprise you by doing this simple logic myself first. But I need to refer to you still...
                          Okay what I can think is something like:
                          ex:
                          if (ndigits[1] == ndigits[0])
                          {
                          num=regenerate random number;
                          ndigits [1];
                          }
                          How is that?
                          I got it....Yeah :) Thanks to you.
                          Code:
                           for (int i = 0; i < 40; i++) 
                             {
                                         int num = (int)(Math.random() * hundredMillion);
                                         if(million < 1000000) 
                                         {
                                          num = num + million;
                                         }
                                         ndigits[i] = num;
                                                        
                                          if (ndigits[i] == ndigits[i-0]) 
                                          {       
                                             num = (int)(Math.random() * hundredMillion);
                                             if(million < 1000000) 
                                             {
                                              num = num + million;
                                             }
                                             ndigits[i] = num;                  
                                          }          
                             }

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #14
                            Originally posted by shana07
                            I got it....Yeah :) Thanks to you.
                            Code:
                             for (int i = 0; i < 40; i++) 
                               {
                                           int num = (int)(Math.random() * hundredMillion);
                                           if(million < 1000000) 
                                           {
                                            num = num + million;
                                           }
                                           ndigits[i] = num;
                                                          
                                            if (ndigits[i] == ndigits[i-0]) 
                                            {       
                                               num = (int)(Math.random() * hundredMillion);
                                               if(million < 1000000) 
                                               {
                                                num = num + million;
                                               }
                                               ndigits[i] = num;                  
                                            }          
                               }
                            Opss...I've checked...nope. ..still not unique number :P, please help to check it

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by shana07
                              I got it....Yeah :) Thanks to you.
                              Code:
                               for (int i = 0; i < 40; i++) 
                              {
                              int num = (int)(Math.random() * hundredMillion);
                              if(million < 1000000) 
                              {
                              num = num + million;
                              }
                              ndigits[i] = num;
                               
                              if (ndigits[i] == ndigits[i-0]) 
                              { 
                              num = (int)(Math.random() * hundredMillion);
                              if(million < 1000000) 
                              {
                              num = num + million;
                              }
                              ndigits[i] = num; 
                              } 
                              }
                              if (ndigits[i] == ndigits[i-0]) is just the same as
                              if (ndigits[i] == ndigits[i]) which will always be true and so does not help you at all. What you need is a separate method called, say, isInArray which looks like this

                              Code:
                               
                              public static boolean isInArray(int[] array, int num) {
                              //do some logic here to determine if 
                              //the int num is in the array called array
                               
                              }

                              Comment

                              Working...