Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random integer generator question

 
Travis Hagan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic