File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Random integer generator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random integer generator question" Watch "Random integer generator question" New topic
Author

Random integer generator question

Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
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

Joined: Mar 24, 2008
Posts: 3858

How is your sizeCheck's gets initialized? Apart from that I can't see anything wrong which wouldn't give the expected result.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
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.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
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

Joined: Oct 13, 2005
Posts: 40028
    
  28
It didn't mean to remove the line; it meant to move it to before the loop.
Travis Hagan
Greenhorn

Joined: Dec 21, 2008
Posts: 12
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

Joined: Oct 13, 2005
Posts: 40028
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random integer generator question