Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating Random Number

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to Generate random number without using function ? can anyone tell me the logic ..
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without using a function method? Why you don't want to use Math.random()
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random Numbers between 0 and what?
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Harsha Smith : it can be any range depend on user input.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understand how it is done in java.util.Random
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without using ANY methods?

I'm not sure that's possible. How are you going to get the user input without using a method?
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@fred rosenberger : Yeah ... Even I am also not getting . this question is asked to my friend in an Interview .
 
Campbell Ritchie
Sheriff
Posts: 51448
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51448
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
random number is between 0.0 to 1.0
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suresh krishan wrote:random number is between 0.0 to 1.0

inclusive or exclusive?
 
Campbell Ritchie
Sheriff
Posts: 51448
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic