Results 1 to 16 of 16
  1. #1
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default I need help generating random numbers for a fill array

    see below for code
    Last edited by william979; 05-03-2011 at 09:23 PM. Reason: mistake

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags.

    [code]
    YOUR CODE HERE
    [/code]

    Also, there is no question here, all you did was dump your code on us. I'm not likely to look through formatted code to find what exactly is your problem. I definitely don't want to read through un formatted code.

    Please edit your post to wrap your code in code tags and make it formatted for easier reading. Also, please ask a specific question. What's wrong? Errors? IF so, copy and paste the exact error in code tags. Logic errors? If so, what happens? What did you expect to happen?

  3. #3
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    i do not understand how to use code tags

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    If only he told you... :rolleyes:

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Type this
    [code]

    Then after that tag paste your code into the text area.

    Then finally type this
    [/code]

    It will look like this
    Java Code:
    YOUR CODE HERE

  6. #6
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    Also when I run the program I do not get any errors, instead I get no values for my display results

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What about generating random numbers are you unsure of? You can use a random object to generate them,
    Java Code:
    Random rand = new Random();
    int x = rand.nextInt(100); //generated a random number from 0 - 99
    There are also methods for getting floats, longs, doubles, etc.

    You can also generate with the static method in math. It will look like this
    Java Code:
    Math.random(); //generates a number from  0.0 - 1.0 exclusive
    [0.0. 1.0)
    Play around with how they work in a new small program that looks like this
    Java Code:
    public class RandomTest{
      public static void main(String[] args){
        Random rand = new Random();
        //print different method calls and ranges
        Math.random();
        //print different values like this
        System.out.println((int)Math.random() * 100);
      }
    }
    You can specify a range with the random object by adding subtracting multiplying to it.

    Java Code:
    int x = random.nextInt(80) + 20;
    //x is 20 - 99

  8. #8
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    I think i am just lost in my own head. Have to have this done by tomorrow. After looking at my program what would be your best answer in how to incorporate a random number generator to do the things I need it to

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A nested loop which generated the scores for each student. A more oop approach would be better, but if this is the student array
    Java Code:
    int[][] students;
    After it is initialized with the number of students and tests per student you can loop through and put random numbers in each spot.

    Java Code:
    loop
      loop
        add random number
      end loop
    end loop

  10. #10
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    am i inputting this ^^^ correctly

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you tried testing it to see what happens?

  12. #12
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    yea nothing..any suggestions???

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't think you quite understand 2 d arrays. Each element in a 2d array is itself an array.

    Java Code:
    int[][] x = new int[5][5];
    is an array that looks like this:
    Java Code:
    x[0] = int[5]
    x[1] = int[5]
    x[2] = int[5]
    ...
    x[5] = int[5]
    So you can't store the id and the scored in the 2d arrays unless you make it an array of score amount + 1. The first element will be the student id, the rest will be randomly generated scores. It would be much simpler to create a student class however.
    Last edited by sunde887; 05-03-2011 at 10:20 PM.

  14. #14
    william979 is offline Member
    Join Date
    Apr 2011
    Location
    TX
    Posts
    8
    Rep Power
    0

    Default

    my biggest problem is trying to understand the basics. I am used to c++ and actually have this program completed.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The only thing that should differ from c++ and java is the method to generate random numbers.

    Just have the method return a 2d array. Create an array, fill it with random numbers, and then return the array.

    Java Code:
    int[] fill singleDimension(int n){
      int[] arr = new int[n];
      for(int i = 0; i < arr.length; ++i){
        arr[i] = //generate random number here
      }
      return arr;
    }
    The 2 d version is very similar to this.

  16. #16
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I often use the Arrays2-class of Bruce Eckel (Chapter 11, Collections, of the free 3rd edition of Thinking in Java) for filling arrays (with small own adaptations).
    It is only worth it though if you fill random arrays of all sorts (datatypes) and different boundaries a lot.

Similar Threads

  1. Generating random numbers
    By chinedum in forum New To Java
    Replies: 9
    Last Post: 09-21-2011, 05:03 AM
  2. Replies: 6
    Last Post: 10-03-2010, 08:25 AM
  3. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 02:38 PM
  4. Replies: 12
    Last Post: 09-23-2009, 09:14 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •