Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Do not use java built in Random Function to generate a random number. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Do not use java built in Random Function to generate a random number." Watch "Do not use java built in Random Function to generate a random number." New topic
Author

Do not use java built in Random Function to generate a random number.

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Language Core Java 1.5 (do not use Random function of java)

Write a methodA that returns a random number between 0 to 3, Use the same methodA to return a random number between 0 and 7. (do not use Random function) .This is a question which comes under uniform distribution principle.

Assume i write a methodA and i have to dynamically pass either 0 or 1 or 2 or 3 to the same methodA,this methodA should display a number between 0 to 7

Rand3(int input) should return dynamically either 0 or 1 or 2 or 3
The same Rand3(int input) should then return any value between 0 to 7.

Language is Core Java 1.5
Solution in Core Java Needed







When The Going Gets Tougher,The Tougher gets Going
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Deepak Lal wrote:Write a methodA that returns a random number between 0 to 3, Use the same methodA to return a random number between 0 and 7. (do not use Random function) .This is a question which comes under uniform distribution principle.
Assume i write a methodA and i have to dynamically pass either 0 or 1 or 2 or 3 to the same methodA,this methodA should display a number between 0 to 7

No, I don't think that what the question asks for. What it asks is that you write a methodA that can return EITHER a random number between 0 and 3 OR between 0 and 7, presumably depending on some value that you pass to it. I presume also that they want the method to use the same code to do it.

The fact is that writing a good pseudo-random number generator is not simple; but there are several poor but simple alternatives (eg, clock time) that might suffice for your needs.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

can anyone please post a working solution in java.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Deepak Lal wrote:can anyone please post a working solution in java.

After 500 posts and nearly 5 years on the ranch surely you know that that is not going to happen.

Assuming we are only talking about integer values, the easiest way to do this is to add the range of numbers to a List, shuffle the list (see the Collections class) and then return the first number from the list.

Joanne
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

So, is this a homework question? What is the goal of the homework question; are you supposed to write your own random number generator?

Without knowing why your question is like this, we can't give you any answer that is what the person who gave you this assignment wants from you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Joanne Neal wrote:Assuming we are only talking about integer values, the easiest way to do this is to add the range of numbers to a List, shuffle the list (see the Collections class) and then return the first number from the list.

But doesn't shuffle() use an RNG?

Winston
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Winston Gutkowski wrote:But doesn't shuffle() use an RNG?


Obviously, a lot depends on the legal interpretation of the requirements. Is it to not use Random, so you can use SecureRandom?

Or does the assignment really mean to dig into Knuth and implement your own pseudo-random generator? Its easy to make one that sort-of-works.

You could, for example, use SHA1 to generate bytes, cast them to an Integer and then apply a suitable sizing function.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do not use java built in Random Function to generate a random number.
 
Similar Threads
next step in craps program
Math.random question
Powerball program for school, Need to to run program 8 times.
Implementing Probability in Java ?
for loop keep stoping