• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Generating Random Number

 
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to Generate random number without using function ? can anyone tell me the logic ..
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without using a function method? Why you don't want to use Math.random()
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 - https://coderanch.com/t/522167/java/java/Create-Random-Number#2365442
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random Numbers between 0 and what?
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understand how it is done in java.util.Random
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@fred rosenberger : Yeah ... Even I am also not getting . this question is asked to my friend in an Interview .
 
Marshal
Posts: 79648
380
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
random number is between 0.0 to 1.0
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

suresh krishan wrote:random number is between 0.0 to 1.0


inclusive or exclusive?
 
Campbell Ritchie
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic