• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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






 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please post a working solution in java.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic