File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Create Random Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Create Random Number" Watch "Create Random Number" New topic
Author

Create Random Number

sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
What is concept to generate random number without using java.util.scanner/math.random or any other class.i mean to say how we can make any program which give us random number....??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


Not sure what you are asking.... are you asking how does the Random class generates a random number?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
Henry Wong wrote:
Not sure what you are asking.... are you asking how does the Random class generates a random number?

Henry

thanks for response
yes i have same question that how Random class generate random no.?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

The Random class uses a pseudo random number algorithm to generate a series of numbers that approximate random numbers -- it is pure mathematics. There is nothing random about these numbers; given the same starting number (seed) it will generate the same exact series of numbers, every time. It is completely deterministic.

By default, it uses the current time as the seed... so, the series of numbers won't be the same, since the time changes.

Henry

sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
thanks
but what is seed there??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Generating random numbers in software is a complicated subject. There is a whole list of algorithms to do this, based on mathematical theorems. Many of those algorithms start from some starting number called a seed.

See Pseudorandom number generator for detailed information.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
thanks
But if i want to make a program to generate random no. without using any function,how should i start to approach that?

sparsh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

sparsh khandelwal wrote:
But if i want to make a program to generate random no. without using any function,how should i start to approach that?


The link provided by Jesper have some ideas on how to write your own. Remember, when writing one of your own, there is no exact right answer -- just test it with lots of samples, and if the distribution looks random (and even distributed) then you succeeded.

Henry
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
thanks
but i am a beginner in java and i need some more stuff for create random number generator,so please provide me some more stuff so that i can easily understand the thing.
thanks in advance!!!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why do you want to create a random number generator? It isn't something beginners (or experienced programmers) do. Creating one isn't hard but creating a good one is extremely hard.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Wouter Oet wrote:. . . It isn't something beginners . . . do. . . .
In which case I shall move this thread.
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
please provide me some more stuff,i want to create random number generator.!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

sparsh khandelwal wrote:please provide me some more stuff,i want to create random number generator.!


What sort of "stuff" are you expecting? You have a link that explains how random number generators work. What else do you want, exactly?


[Jess in Action][AskingGoodQuestions]
sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70
it is in compact form,i need everything in detail.!
Because i am at the stage on which i not know how to take the current time as seed too.
so please recommend me (books,notes,tutorial) which is fully dedicated to create random number generator.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1368
Try Apache commons random generator
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

The Wikipedia link is an excellent starting point to do research on algorithms to generate random numbers. If you really want to implement your own random number generator, you'll have to start reading the article and do some more research. The article List of random number generators contains a list, with links and explanations in as much detail as you want, of different algorithms.

You'll need to study those and determine which one is suitable for your purposes, and then write Java code that implements your chosen algorithm.

It's not a question that you can solve in five minutes, and don't expect that someone will post the complete code here so that you don't have to put any effort into it...

sparsh khandelwal wrote:Because i am at the stage on which i not know how to take the current time as seed too.

You get the current time (in milliseconds since 1 January 1970, 00:00:00 UTC) in Java by calling System.currentTimeMillis(). Study the algorithms to understand what you're supposed to do with the seed.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1368
Check this link for Apached commons random generator.
http://commons.apache.org/math/apidocs/org/apache/commons/math/random/RandomGenerator.html
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

sparsh khandelwal wrote:it is in compact form,i need everything in detail.!
Because i am at the stage on which i not know how to take the current time as seed too.
so please recommend me (books,notes,tutorial) which is fully dedicated to create random number generator.

I don't think you understand how we do things here. We don't hand out code. We are here to help you learn. We will answer specific questions as best we can. However, a general, overly broad "I need you to tell me everything" will not get you much of a response around here.

Do you know how to write a "hello world" application? Can you compile and run it?

Do you know how to write java code at all? Do you know how to write modular programs? Do yo know how to analyze a problem and break it down into it's components?

IMHO, getting the current time as the seed would be one of the LAST things you'd want to do. I would think you'd want to use the same seed each time during your development process, so you can see how good or bad your algorithm is.

Show us what you have so far, ask SPECIFIC and FOCUSED questions, and you'll get all kinds of help. Continue saying "Tell me everything I need to know about how to do this" will most likely be met with silence. Reply when people ask you questions. Read the articles people point you to, and acknowledge you have read them.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create Random Number
 
Similar Threads
Random Number Chooser programming exercise
Query about serialVersionUID
Better way of random number generation using multiple threads
Math.random VS random.nextDouble
Generating Random Number