• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random number error

 
peiyoon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
well.
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

regards,
peiyoon
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited September 13, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peiyoon,
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.
Thanks,
Cindy
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic