Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default Array of random numbers ...

    hey all how are you ?

    a little problem I have , well I have to make an array which generates 50 numbers from 0 to 999 which I can manage that the problem lies with the part were the numbers generatet cant be the same here is what ive done so far

    Java Code:
    import java.util.Random;
    public class ArrayRandom{
        public static void main(String args[]
            {
                Random r = new Random();
                int temp;
                int arr[] = new int[50];
                for(int i = 0; i < 50; i++)
                            {
                           
                            temp = r.nextInt(999) + 1; // this will give you a random number between 0 to 999.
                                                           
                            arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]
                            }
                            for(int i = 0; i < 50; i++)
                            {
                                   System.out.print(arr[i] + " ");
                            }
                    }
    if some one could help me this would be a great help thanx shane : )

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    temp = r.nextInt(999) + 1; // this will give you a random number between 0 to 999.
                                                           
    arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]

    In between these lines you are expected to check to see that temp is a number that has not been generated before. Think about:

    * how you are going to do that. (Imagine you are the computer and use a piece of paper to record the array values. Say the number was generated was 42. How would you decide if this was a new number?). When you have decided on a process you can start expressing it in code. It is quite possible that this may involve yet another for loop.

    * What are you going to do if the number is not unique. Ie if you have seen it before.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    only a little note:

    temp = r.nextInt(999) + 1; // this will give you a random number between 0 to 999.
    this is not correct.
    read the api doc:
    "the next pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive) from this random number generator's sequence"

    nextInt(999) --> 0-998 + 1 = 1-999
    nextInt(1000) --> 0-999

  4. #4
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    what is an api doc everyone keeps telling me this thanx for you'r help aswell im new to java : )

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You are looking for the documentation for the java.util.Random class (the classes are listed down the left hand side) to check what it says about nextInt().

    If you replace 999 with 1000 you will get a number between 1 and 1000 inclusive. So you might want to something about the +1 as well.

    You also might want to check whether the desired range is between 0 and 999 inclusive or 1 and 999 inclusive. Often this question gets asked using the latter range, although your case may be different.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Write a program called Random50 that fills an array with fifty random values between 1 and 999

    OK, so now we have the range. Are you clear about how to use nextInt() to get numbers in this range?

    Once you are, you can think about the duplicates: how to detect them, and what to do once you find one.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  10. #10
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    why is this a problem im looking for someone to treat me like a thicko cuz thats what i am in java all the other treat me like im a pro

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    No one wants to waste time working out someone's problem when it's already been solved elsewhere in a cross-post. Ignoring this implies that the poster doesn't respect the time commitment made by the volunteers of the various forums. The best thing to do is to notify all if you've made cross-posts and where you've made them. This forum is no different from any of the others in this regard.

  12. #12
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    ok so if i wanna stay on this forum i just tell the rest i dont need there help yeh im sorry i will correct this now

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by shane1987 View Post
    ok so if i wanna stay on this forum i just tell the rest i dont need there help yeh im sorry i will correct this now
    No, all you need to do is notify all locations of every cross-post.

  14. #14
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    ok i have done it im sorry for this didnt realize the outcome was like this

  15. #15
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    OK, so now we have the range. Are you clear about how to use nextInt() to get numbers in this range?

    Once you are, you can think about the duplicates: how to detect them, and what to do once you find one.

    Hi sorry about cross posting ive informed all other sited and I do apreichiate your help for me also im not clear about the nxt.Int()
    so can we start again ? :o

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    We've figured out what the range is and - I hope - untangled the cross post mess as much as it can be.

    If you think the approach may be any good, I would suggest going back to the two points I made in my first post. I'll repeat the first of them:

    how you are going to do check that temp has not been seen before. (Imagine you are the computer and use a piece of paper to record the array values. Say the number was generated was 42. How would you decide if this was a new number?). When you have decided on a process you can start expressing it in code. It is quite possible that this may involve yet another for loop.

    Notice that there's a question in there!

  17. #17
    razzle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Heya, I've got this exact same problem with my assessed work, I can generate the random numbers, etc. I'm just having a load of trouble wrapping my head around this checking business. At the moment I've got the idea of using a while loop inside the while loop generating the random numbers, with an if statement inside that testing but that's when I get stuck.

    while (numbers[arrayPos] <= count)
    {
    if randNum != number[arrayPos]


    }

    now after the if statement I don't know what to put... i want it to check all the values first before I do something... Sorry to put this in your thread shane1987, but I think we might actually be in the same class :P

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You really should start your own thread. In general terms you might need a variable whose value gets set by the setting loop to say whether the number is good. (Or another method that is passed the number and the array and returns a boolean value to say whether or not to add the number.)

  19. #19
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    ha ha most probs lol :)

  20. #20
    shane1987 is offline Member
    Join Date
    Nov 2010
    Location
    wales
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
       1.
          import java.util.Random;
       2.
          public class ArrayRandom
       3.
          {
       4.
          public static void main(String args[])
       5.
          {
       6.
          Random r = new Random();
       7.
           
       8.
          int temp;
       9.
          int arr[] = new int[50];
      10.
          for(int i = 0; i < 50; i++)
      11.
          {
      12.
         
      13.
          temp = r.nextInt(1000); // this will give you a random number between 0 to 999
      14.
           
      15.
           
      16.
          zeroStr = "000" //string with 3 zeroes.
      17.
           
      18.
          int r = String.valueOf(array[i]).length(); // get the length of element i
      19.
           
      20.
          if (r < 3) { // for digits less than 3
      21.
           
      22.
          String temp = zeroStr.substring(0, 3 - r) + String.valueOf(array[i]); // concat the zeroes together with the digit
      23.
           
      24.
          System.out.println(temp);
      25.
           
      26.
          }
      27.
           
      28.
          arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]
      29.
          }
      30.
          for(int i = 0; i < 50; i++)
      31.
          {
      32.
          System.out.print(arr[i] + " ");
      33.
          }
      34.
          }
      35.
           
      36.
           
      37.
          }

    i no ive missed a step just wanted to no is this what you do to ensure the there is 3 digits at all time

Page 1 of 2 12 LastLast

Similar Threads

  1. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 12:59 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  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
  •