1 2 Previous Next 27 Replies Latest reply: Nov 25, 2009 11:03 AM by YoungWinston Go to original post RSS
      • 15. Re: Random number generator
        843789
        import java.util.Random; //gets the Random class
        public class Random100 
        {
             public static void main(String args[])
             
             {
                  int x; 
                  int [] Arrayofrandom = new int [100];//defines length  and declares array
                  Random RandomGenerated = new Random();//generates random number
                  
                  
                  
        
                  for (x = 0; x<Arrayofrandom.length;)
             
                  {     
                  
                       if (RandomGenerated!=RandomGenerated)
                       Arrayofrandom[x] = RandomGenerated.nextInt(999);//tells only numbers 0-999
                       x++;
                       
                  }
                  
                  
        
        
                  
                  
                  
        
        
                  for (x=0; x<100;x++)
                  {
                                 
                       if (Arrayofrandom[x]<10)
                            System.out.print("00"+Arrayofrandom[x]+"\t");
                       else if (Arrayofrandom[x]<100&&Arrayofrandom[x]>9)  //heads numbers less than 100 with 0's
                            System.out.print("0"+Arrayofrandom[x]+"\t");
                       else
                            System.out.print(Arrayofrandom[x]+"\t");
                  }
        
                  
             
             }
        }
        ive done that now buts its just print all 0's :( im sure i followed your pseudo code as best as i could, where abouts have I gone wrong in my code?
        • 16. Re: Random number generator
          3004
          What is this supposed to do? And how to you expect it to ever be true?
          if (RandomGenerated!=RandomGenerated)
          Do you know how to check if a certain value is contained in an array? I'd put that bit in a separate method if I were you.

          And you're still not using curly braces with every if, for, and while like you should be.

          Also, nexInt(999) will give you a number in the range 0..998. Is that what you want?

          Edited by: jverd on Nov 24, 2009 1:51 PM
          • 17. Re: Random number generator
            843789
            no i dont know how to check if number is in an array
            • 18. Re: Random number generator
              3004
              James-----m wrote:
              no i dont know how to check if number is in an array
              Well, how would you do it "by hand"? You have 100 boxes. You know that the first 8 have cards in them with numbers written on the cards. You want to know if the number 15 is on any of those cards. What are the exact steps you'd follow? The precise, exact, simple, basic steps.
              • 19. Re: Random number generator
                843789
                if(Arrayofrandom[x]<10)
                                    System.out.print("00"+Arrayofrandom[x]+"\t");
                               else if(Arrayofrandom[x]<100&&Arrayofrandom[x]>9)
                                    System.out.print("0"+Arrayofrandom[x]+"\t");
                               else if(Arrayofrandom[x]>100)
                                    System.out.print(Arrayofrandom[x]+"\t");
                Instead of this if statement, use a DecimalFormat object to pad the zeros.
                • 20. Re: Random number generator
                  796447
                  Also the 2nd part of this "if" clause is superfluous, since you have already determined it is necessarily true. Otherwise this statement would not be getting evaluated at all due to the preceding "if", where Arrayofrandom[x] is already known to be not less than 10 (so it must be greater than 9, silly):
                  else if(Arrayofrandom[x]<100 *&&Arrayofrandom[x]>9* )
                  Also, what about the case if Arrayofrandom[x] == 100? You missed it.
                  You would cover it if you would have simply replaced this part:
                  else if(Arrayofrandom[x]>100)
                  with:
                  else // simply else, not another if.
                  // if it gets to this point, it is necessarily the case that
                  // Arrayofrandom[x] is >= 100
                  • 21. Re: Random number generator
                    843789
                    warnerja wrote:
                    You would cover it if you would have simply replaced this part:
                    else if(Arrayofrandom[x]>100)
                    with:
                    else // simply else, not another if.
                    // if it gets to this point, it is necessarily the case that
                    // Arrayofrandom[x] is >= 100
                    That would be my fault for copying the wrong version of the if statement.
                    • 22. Re: Random number generator
                      796447
                      flounder wrote:
                      That would be my fault for copying the wrong version of the if statement.
                      Oops. Um yeah, I see his version in reply #5 was wrong like that but improved later on.
                      • 23. Re: Random number generator
                        843789
                        for (x = 0; x < 100; x++) 
                                  {
                                      
                                      do 
                                  {
                                       a = 0;
                                       r = RandomGenerated.nextInt(999);
                                  
                                  for (z = 0; z < x; z++)
                        
                                           if (Arrayofrandom[z] == r)
                                       a = 1;
                                      }      
                                  
                                  while (a==1);
                        
                                           Arrayofrandom[x] = r; //makes sure number is unique
                                  }
                        I did it using that code instead thanks for ur help though people =]
                        • 24. Re: Random number generator
                          3004
                          James-----m wrote:
                          I did it using that code instead thanks for ur help though people =]
                          Instead of what? That's almost exactly what I was telling you to do.

                          I have a few suggestions, however.

                          - x, a, and z are not good varaible names. They convey nothing about what the variable represents.

                          - You initially said you want numbers in the range 0..999. Your code only produces numbers in the range 0..998.

                          - Since a can only have two values, and since its meaning is that something has or has not happened, it should be a boolean (and have a more meaningful name, of course).

                          - By convention, variable names start with lower case and use camel case within. (randomGenerated, arrayOfRandom).

                          - The name RandomGenerated is misleading, since the object it refers to is not a generated number. Just "random" would be fine.
                          • 25. Re: Random number generator
                            YoungWinston
                            James-----m wrote:
                            it heads all the numbers below 100 with 0's eg 099 and 003.
                            Have a look at String.format() or System.out.printf() (both use the same notation).

                            Winston
                            • 26. Re: Random number generator
                              YoungWinston
                              James-----m wrote:
                              I did it using that code instead thanks for ur help though people =]
                              A couple of tips to go with the other good advice:
                              1. Resist the urge to code everything in one place. Problems are best handled by breaking them up and putting them in different methods; and your "does my array contain this value?" part is a classic case in point.
                              2. Jverd's [original reply|http://forums.sun.com/thread.jspa?messageID=10871918#10871918] suggested that you "Create a List". I suggest you have a look at the List API (and possibly one of its implementations, like ArrayList), because it contains exactly the things you need for your program. One of the basic precepts of OOP is "don't re-invent the wheel".

                              Winston
                              • 27. Re: Random number generator
                                3004
                                YoungWinston wrote:
                                1. Resist the urge to code everything in one place. Problems are best handled by breaking them up and putting them in different methods; and your "does my array contain this value?" part is a classic case in point.
                                Good point. I mentioned that earlier, but I forgot to reiterate it in my previous reply. Thanks.
                                1 2 Previous Next