• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random integer generator question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, I had a quick question that I hope someone could provide an answer to. I created this program to fill an array with integers. All it does is randomly generate (or psuedo randomly) a number and store it in the first box and then second... until I run out of boxes. Usually after running this once or twice I get a bunch of zeros. Any ideas why???

Here's the code in case that is any more revealing:



PS: if there is a better way to generate random numbers, I'm all ears.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is your sizeCheck's gets initialized? Apart from that I can't see anything wrong which wouldn't give the expected result.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Travis Hagan wrote:Howdy, I had a quick question that I hope someone could provide an answer to. I created this program to fill an array with integers. All it does is randomly generate (or psuedo randomly) a number and store it in the first box and then second... until I run out of boxes. Usually after running this once or twice I get a bunch of zeros. Any ideas why???

Here's the code in case that is any more revealing:



PS: if there is a better way to generate random numbers, I'm all ears.


Try to move line 7 out of while loop. It should work.
 
Travis Hagan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed line 7 but it didn't work. Null pointer exception. Turns out that after running certain methods the size check would be too high so that when I ran the iteration it created the array but didn't fill.

Sometimes things are more simple than they appear.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It didn't mean to remove the line; it meant to move it to before the loop.
 
Travis Hagan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I missed that. I get in the habit of reading way too fast. It works either way, and moving out the loop should save the PC a minimalistic amount of time but all the same.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Minimal? Not for Random. The real problem with Random (much less of a problem with Java5/6 than with Java1.4) is that when you create two instances, there is a risk of them both producing the same sequence of numbers. So you ought to avoid creating new Random objects, and use the same instance, as far as possible.

By the way: if you look in the Random API it says java.lang.Math.Random() is often easier to use.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic