This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic


Nickk Singhal

Joined: Dec 09, 2011
Posts: 4
how to generate random number without using random class
Joanne Neal

Joined: Aug 05, 2005
Posts: 3720
You should read NotACodeMill and then update your post.

NB - this was a reply to the original post which was just a question copied from a book/exam paper/whatever and if i remember properly had nothing to do with random numbers.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278

You could implement your own random number generator. But beware that it is not easy to create "good" random numbers (random numbers which are truly random, or which at least have a statistical distribution that comes close to being really random). Several algorithms have been invented to generate pseudo-random numbers; do some research to learn more about them.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 19723

Or just use Math.random(). It generates a double value between 0.0 (inclusive) and 1.0 (exclusive), so multiply it by X and cast to int to get an int value between 0 (inclusive) and X (exclusive).

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Arrays