13 Replies Latest reply: Dec 11, 2009 1:05 AM by 843789 RSS

    loading an array using math.random

    843789
      just wondering what the syntax is for loading an array of ints using the math class random method.
        • 1. Re: loading an array using math.random
          843789
          Exactly the same as loading any array with any data.

          To add:

          Do you know haow to access an array and a specific location in an array?
          Do you know how to make an assignment?

          If so then it seems the only problem would be generating the random numbers. See the Random class.

          Edited by: flounder on Dec 11, 2009 10:35 AM
          • 2. Re: loading an array using math.random
            3004
            Also, you'll probably want to use java.util.Random, rather than math.random.
            • 3. Re: loading an array using math.random
              843789
              Myself I'd use a for loop to iterate through the array, and I wouldn't use Math class but instead use a Random class object's nextInt(MAX_VALUE) where max value -1 is the top value any of your random numbers can take.

              For more help, why not give it a try on your own, post your code (with code tags) and we can work from there.

              Much luck!

              edit: I'm slow as molasses today!

              Edited by: Encephalopathic on Dec 10, 2009 3:37 PM
              • 4. Re: loading an array using math.random
                843789
                public static loadArray(int num2, int num 3, int[] intArray)
                     {
                          public static random(
                     
                     }
                im unsure of how to do it. i need to use random class to do it as well.
                • 5. Re: loading an array using math.random
                  843789
                  Yes, you need to create an object of the Random class and then call it's method(s). Have you covered objects and method calls yet?
                  • 6. Re: loading an array using math.random
                    843789
                    yes, but syntax is always my problem.

                    is my code code correct in syntax?
                    • 7. Re: loading an array using math.random
                      843789
                      Does it look like any examples you have seen?
                      Have you compiled and see what the compiler thinks?
                      • 8. Re: loading an array using math.random
                        843789
                        seems the compiler has given me 9 problems with these few lines already.

                        i can't find any example to particularly help me. got any?
                        • 9. Re: loading an array using math.random
                          843789
                          jtan wrote:
                          seems the compiler has given me 9 problems with these few lines already.
                          The first thing compilers do is check the syntax. It produces an error message for each syntax issue and won't proceed if your program isn't syntactically correct.

                          So if you're getting compiler errors, read them to see what's wrong with your program. If you don't understand a compiler error message, then post it here and ask about it. Don't just throw your hands in the air and ask how to fix the program. Use the compiler messages; they're here to help you.
                          i can't find any example to particularly help me. got any?
                          There are tons of tutorials on Sun's web site. And all over the internet. Google is your friend.

                          And don't you have a textbook?

                          You can use the language spec if you want a really formal description of the syntax, but I suspect that you're unfamiliar with the standards for specifying syntax.
                          • 10. Re: loading an array using math.random
                            843789
                            You don't need any examples.

                            You know how to declare variables.
                            You know how to create an object.
                            You know how to create an array
                            You know how to call methods.
                            You know how to write a loop.
                            You know how to assign values.

                            That's about all there is. These are your building blocks. Now all you need to do is to put them together to do what you want. So take it one step at a time. Create a Random object. Write a loop that prints out 5 random numbers. Go from there.
                            • 11. Re: loading an array using math.random
                              3004
                              Do you understand that generating a random number and putting a value into an array are to separate and completely independent steps?

                              If not, then I can't help you.

                              If you do understand that difference, then which part are you having problems with? What problems? What did you try, and where did you get stuck or what errors arose?
                              • 12. Re: loading an array using math.random
                                3004
                                jtan wrote:
                                seems the compiler has given me 9 problems with these few lines already.
                                Okay, that's nice.

                                Are you just making conversation, or did you expect somebody to help you fix your errors with no more information that, "the compiler has given me 9 problems"?
                                i can't find any example to particularly help me. got any?
                                Examples of what? Your whole homework assignment? No, and even if I had one, I wouldn't give it to you. Examples of some smaller piece, like generating a random number? Yes, you can find those on the web. Google exists.
                                • 13. Re: loading an array using math.random
                                  843789
                                  Why? If the basis of your problem lies solely on syntax then the compiler itself should be more than enough help. By the way make sure you add semicolons at the end of your statements, capitalize the first letter of the first word of your sentences, and dot your "i"'s.

                                  If the whole point of your homework assign is to write this "overly simple" Java program that fills up an array using random() then if we give any code it'll probably kill the purpose if you don't know the syntax and rather just copy paste. Because the example code would be only a few lines long.