0

Im wondering how to print all numbers inbetween a set value in an array with random numbers. It should also state how many of the users random numbers are above the set value. Here is an example of how it could look:

"How many random numbers between 0 – 999 do you want? 12

These are your numbers

998 168 859 32 533 390 339 243 328 164 536 375

these 8 numbers is between 0 – 499

168 32 390 339 243 328 164 375

these 4 numbers is between 500 – 999

998 859 533 536"

this is my code so far, i have managed to get it to print the largest number.

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);                                                    
    System.out.print(" How many random numbers between 0 – 999 do you want? ");     
    int varde = in.nextInt();   
    System.out.println(" ");
    System.out.println(" this is your numbers:");

    int randomArray[]=new int[varde]; 

    for(int i = 0; i<varde; i++)
    {   
        randomArray [i] = (int) (Math.random () * 999); 
        System.out.println(randomArray[i]);
    }

    int largest = randomArray[0]; 
    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>largest){ 
            largest =  randomArray[x]; 
        } 
    } 

    System.out.print("Largest no is: "+largest); 
}
PakkuDon
  • 1,627
  • 4
  • 22
  • 21
  • What have you tried so far? Showing existing code for other unrelated tasks is not "what you have tried so far". – Jason C Apr 13 '14 at 14:44
  • 1
    Minor note: `Math.random()` returns values between `0` (inclusive) and `1` (exclusive) so `(int) (Math.random () * 999); ` will not random numbers between `0`-`999` but `0`-`998` (you could random `999` only if `Math.random()` would return `1` which is impossible). Try instead multiply by 1000. You can also try using `Random` class. It has nice method `nextInt(max)` which will random between `0`(inclusive) and `max`(exclusive) so `nextInt(1000)` would work fine for you. – Pshemo Apr 13 '14 at 14:47
  • 2
    @Andrei [No it doesn't.](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/Math.java#Math.random%28%29) – Jason C Apr 13 '14 at 14:52
  • What i have i tried so far is the code im showing, i thought that i could use what i managed to pick up from the book "the largest number" and somehow work with that so it instead displays the largest numbers above a certain point. Thanks for the pointers on Math.random! – user3529167 Apr 13 '14 at 15:59

1 Answers1

1

There you go.

What i did was add the java.util.Random class instead of Math.random() for random number generation.

Used an ArrayList that allocates more memory if it needs more, to store the values that are lower or greater than 499.

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);                                                    
    System.out.print(" How many random numbers between 0 – 999 do you want? "); 
    int varde = in.nextInt();   
    System.out.println(" ");
    System.out.println(" this is your numbers:");
    Random randomGenerator = new Random();

    int randomArray[]=new int[varde];

    for(int i = 0; i<varde; i++)
    {   
        randomArray[i] = randomGenerator.nextInt(1000); 
        System.out.print(randomArray[i]);
    }

    int largest = randomArray[0]; 
    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>largest)
            largest =  randomArray[x]; 
    }
    System.out.println("Largest no is: "+largest); 

    List<Integer> numbersGreaterThanHalf = new ArrayList<Integer>();
    List<Integer> numbersLowerThanHalf = new ArrayList<Integer>();

    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>499) 
            numbersGreaterThanHalf.add(randomArray[x]);
        else
            numbersLowerThanHalf.add(randomArray[x]);
    }

    System.out.println("these " + numbersLowerThanHalf.length() + "numbers is between 0 – 499");
    for(Integer number: numbersLowerThanHalf)
    {
       System.out.print(number + " ");
    }

    System.out.println("these " + numbersGreaterThanHalf.length() + "numbers is between 500 – 999");
    for(Integer number: numbersGreaterThanHalf)
    {
       System.out.print(number + " ");
    }
}

No new arrays implementation extract:

  StringBuilder numbersAboveHalf = new StringBuilder();
  StringBuilder numberBelowHalf = new StringBuilder();
  int countOfNumbersAboveHalf = 0;
  int countOfNumbersBelowHalf = 0;

  for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>499){
            numbersAboveHalf.append(randomArray[x]).append(" ");
            ++countOfNumbersAboveHalf;
        }
        else{
            numbersBelowHalf.append(randomArray[x]).append(" ");
            ++countOfNumbersBelowHalf;
        }
  }
  System.out.println("these " + countOfNumbersBelowHalf + "numbers is between 0 – 499");
  System.out.println(numbersBelowHalf.toString());
  System.out.println("these " + countOfNumbersAboveHalf + "numbers is between 500 – 999");
  System.out.println(numbersAboveHalf.toString());

What this does is iterate over the array and:

  • count how many number above 500 are there and how many are below 500.
  • build 2 strings with the numbers.
Andrei
  • 3,086
  • 2
  • 19
  • 24
  • Did you want to explain how your code supposedly solves OP's problem? Or at least include a few comments? What is OP meant to learn from this? – PakkuDon Apr 13 '14 at 14:46
  • I was editing it to add that...as you can see. Can you please retract your downvote? – Andrei Apr 13 '14 at 14:46
  • I didn't downvote, but I don't think using `java.util.Random` is something to promote over `Math.Random`, given that `java.util.Random` is broken and should not be used for security measures. `Math.Random` is much safer. In this case it's not a big deal, but still, no need to pick up bad habits. Other than that I found your solution to be fine, haven't tested, but from what I can see it should produce what was in the example. – Tobb Apr 13 '14 at 14:51
  • `Math.random()` uses `java.util.Random` inside. Check the javadoc please. Can you elaborate on why it is a security issue ? That is new to me. – Andrei Apr 13 '14 at 14:53
  • 1
    @Andrei It most certainly [does not](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/Math.java#Math.random%28%29) create a new instance every time, only the first time. – Jason C Apr 13 '14 at 14:53
  • 1
    @Tobb When the OP is at the skill level where he is writing cryptographically secure applications, *that* will be the appropriate time to learn about random number generator quality. In this problem right now, it is irrelevant. – Jason C Apr 13 '14 at 14:55
  • @JasonC you are right about that, I edited it out. Still wondering about why the down-vote.. – Andrei Apr 13 '14 at 14:55
  • 1
    @Andrei Re: Security; commonly known, not worth discussing here, see http://stackoverflow.com/questions/11051205/difference-between-java-util-random-and-java-security-securerandom or a Google search for information. – Jason C Apr 13 '14 at 14:56
  • So what i get from this is that i need to create 2 new Array's and have one for the +499 and one for the -499? – user3529167 Apr 13 '14 at 16:10
  • Added implementation without 2 new arrays. – Andrei Apr 13 '14 at 16:21
  • Ok so that makes a little bit more sense to me since we havent worked with lists yet! Thanks alot – user3529167 Apr 13 '14 at 16:25