File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes random number error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random number error" Watch "random number error" New topic

random number error


Joined: Aug 06, 2001
Posts: 4
dear sir,
I am very doubtful in generating a random number within the range selected. for eg, i wants to generate50 random number start from 0000. please advise me!
following is my sample of random in java:

i am using two method:
1) using min and max, my result generated gives me
negative value and duplication of the same value occured as
2) using NumInt, count,result is also the same as above.
I am doubtful in ways to solve duplication occured and negative value. Please advise me..thanks

(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited September 13, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.

"JavaRanch, where the deer and the Certified play" - David O'Meara
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Try the nextInt method in java.util.Random. From the sun site:

public int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability...

[This message has been edited by christopher foran (edited September 13, 2001).]

Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I think that the problem you are having with negatives is due to the fact that the return is defined as an int, and an int in java is -2147483648 to 2147483647.
I agree. Here's the link:
subject: random number error
jQuery in Action, 3rd edition