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 Generating Random Number 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 "Generating Random Number " Watch "Generating Random Number " New topic
Author

Generating Random Number

Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

How to Generate random number without using function ? can anyone tell me the logic ..
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Without using a function method? Why you don't want to use Math.random()
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Subhash Pavuskar wrote:How to Generate random number without using function ? can anyone tell me the logic ..


Did you have a look at Random Number Generation algorithms? You can use any one of them. They are just some mathematical formulations that you can implement in code.

And I think this has been asked before as well in the forum. May be SearchFirst would have helped?


Mohamed Sanaulla | My Blog
Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

@John Jai : You are using function here and i am asking about without using function call.


@Mohamed Sanaulla : I searched and they people used function ,so if you found the problem solution for the same then please let me know.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Subhash Pavuskar wrote:@John Jai : You are using function here and i am asking about without using function call.

Yes - I just wanted to know your purpose. And please refer functions as methods.

Subhash Pavuskar wrote:@Mohamed Sanaulla : I searched and they people used function ,so if you found the problem solution for the same then please let me know.


A search result - http://www.coderanch.com/t/522167/java/java/Create-Random-Number#2365442
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Random Numbers between 0 and what?
Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

@Harsha Smith : it can be any range depend on user input.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Understand how it is done in java.util.Random
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

Without using ANY methods?

I'm not sure that's possible. How are you going to get the user input without using a method?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

@fred rosenberger : Yeah ... Even I am also not getting . this question is asked to my friend in an Interview .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
If it’s an interview question, that changes everything You can remind them what the definition of a function is: for the same input, a function always generates the same result. A random number generator is different; it generates a different result each time. So it isn’t a function.
Math.random() is in fact a function; it does not actually generate random numbers, but numbers whose value is difficult to predict from their seed. You would have to take the java.util.Random class apart to find how it is done. The code is in the usual src.zip file.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
A few minutes ago, I wrote:. . . A random number generator is different; it generates a different result each time. . . .
Imprecise!! A random number can be repeated. I once won £10 by guessing a coin. I stuck on “heads” every time and people said, “What about the law of averages?” There is no such thing as a law of averages, and I won the £10
suresh krishan
Greenhorn

Joined: Oct 13, 2011
Posts: 17
random number is between 0.0 to 1.0
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

suresh krishan wrote:random number is between 0.0 to 1.0

inclusive or exclusive?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
fred rosenberger wrote: . . . inclusive or exclusive?
Semi-exclusive.

That shows how precise you have to be when you work with computers. The java.util.Random#nextDouble() documentation returns a number between 0.0 and 1.0, and it tells you whether it is inclusive or exclusive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating Random Number