This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Generating Random Number Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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: 9939
    
    6

Without using ANY methods?

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


Never ascribe to malice that which can be adequately explained by stupidity.
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: 32599
    
    4
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: 32599
    
    4
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: 12
random number is between 0.0 to 1.0
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

suresh krishan wrote:random number is between 0.0 to 1.0

inclusive or exclusive?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generating Random Number
 
Similar Threads
random number
random numbers, returns a negative number?
I Messed Up With My Thread Code
random number
Arrays