Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

Create Random Number

sparsh khandelwal
Ranch Hand
Posts: 76
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
Marshal
Posts: 21116
78

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

Henry

sparsh khandelwal
Ranch Hand
Posts: 76
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
Marshal
Posts: 21116
78
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
Posts: 76
thanks
but what is seed there??

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
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.

sparsh khandelwal
Ranch Hand
Posts: 76
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
Marshal
Posts: 21116
78
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
Posts: 76
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.

Wouter Oet
Saloon Keeper
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.

Campbell Ritchie
Sheriff
Posts: 48943
60
Wouter Oet wrote:. . . It isn't something beginners . . . do. . . .
In which case I shall move this thread.

sparsh khandelwal
Ranch Hand
Posts: 76
please provide me some more stuff,i want to create random number generator.!

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
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?

sparsh khandelwal
Ranch Hand
Posts: 76
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
Posts: 1463
Try Apache commons random generator

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
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
Posts: 1463
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
Posts: 12123
30
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.