Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Jeanne Boyarsky
• paul wheaton
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Ron McLeod
• Tim Moores
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Vijitha Kumara

# Java greenhorn adding random numbers

Greenhorn
Posts: 6
Please help I am trying to get x below to equal the sum of the first two random integers and not integers 3 and 4. But the program doesn't add togetehr -7 and 12 but instead adds together later random integers that are created to get an answer of 14. What do I need to do? Thanks for any assistance. This is my first time using JAVA and I am struggling.

int rand_int1 = -rand.nextInt(20);
int rand_int2 = rand.nextInt(20);
int x;
x = (-rand.nextInt(20) + rand.nextInt(20));
System.out.println("and this number together: "+rand_int2);

run:
and this number together: 12
BUILD SUCCESSFUL (total time: 0 seconds)

The program will be used to create 20 random integers once it is fully written but I am not able to get the first random numbers to be used in the program.

Ranch Hand
Posts: 99

Phil Bignell wrote:Please help I am trying to get x below to equal the sum of the first two random integers and not integers 3 and 4. But the program doesn't add togetehr -7 and 12 but instead adds together later random integers that are created to get an answer of 14. What do I need to do? Thanks for any assistance. This is my first time using JAVA and I am struggling.

int rand_int1 = -rand.nextInt(20);
int rand_int2 = rand.nextInt(20);
int x;
x = (-rand.nextInt(20) + rand.nextInt(20));
System.out.println("and this number together: "+rand_int2);

run:
and this number together: 12
BUILD SUCCESSFUL (total time: 0 seconds)

Why are you calling rand.nextInt(...) four times? You only need to call it two times.

Phil Bignell
Greenhorn
Posts: 6
I am later using this to produce 20 random numbers but first want to do calculations with each pair of random numbers.

Phil Bignell
Greenhorn
Posts: 6
I assume you mean I call it four times as I use four numbers. If I just want to add 2 numbers together then I assume that I just use   rand.nextInt(2)

Could I then loop through 5 times to get 20 numbers?

Gerard Gauthier
Ranch Hand
Posts: 99

Phil Bignell wrote:I assume you mean I call it four times as I use four numbers. If I just want to add 2 numbers together then I assume that I just use   rand.nextInt(2)

Could I then loop through 5 times to get 20 numbers?

Gerard Gauthier
Ranch Hand
Posts: 99
If you need 20 random numbers, why don't you create an array of integers and populate it with random numbers.. example:

Phil Bignell
Greenhorn
Posts: 6
Thank you for your reply showing how to get 20 random integers in the array. How though can I refer to each integer in the array so I can do separate calculations on each pair of integers?

Gerard Gauthier
Ranch Hand
Posts: 99

Phil Bignell wrote:Thank you for your reply showing how to get 20 random integers in the array. How though can I refer to each integer in the array so I can do separate calculations on each pair of integers?

Just use a for loop and make sure you increment by 2 since you are grabbing a pair of entries.

Phil Bignell
Greenhorn
Posts: 6
Gerard Thank you for your help with this.

Marshal
Posts: 63849
209

Gerard Gauthier wrote:. . .

Ouch! I don't like that at all. I think both of you are confusing the range of the number obtained and how many you are getting. Please have a look at the documentation for Random#nextInt(). Look what the argument passed means, and how many numbers you get, and what range they are in. Also look at this method and other methods with similar names. That returns an IntStream, which has a method directly producing an int[]. You can get a 1000‑element array containing ints, none less than −100 and all less than 100, like this:-You refer to the elements by the usual technique: randoms[i]

Phil Bignell
Greenhorn
Posts: 6
Campbell Thanks I will look through the material..

Campbell Ritchie
Marshal
Posts: 63849
209

Phil Bignell wrote:Campbell Thanks

That's a pleasure

I will look through the material..

Make sure to have a good look at it; you will find that technique very useful.

 A teeny tiny vulgar attempt to get you to buy our stuff global solutions you can do at home or in your backyard https://www.kickstarter.com/projects/paulwheaton/better-world-boo