Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random number generators

 
Abhijit Ajansondkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is abhijit .
I have a method which produces random numbers for me from 1 to 100 . This is the method

Please suggest the code for me to produce using the above function only :-
1. random numbers from 1 to 20 without repitition
2. random numbers from 1 to 200 without repitition using this method only
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far? For instance, what code have you written to handle the "without repetition" section of your questions?
 
Abhijit Ajansondkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Panagiotis Kalogeropoulos !
This is my code but it is producing the numbers < 20 but the number of elements and uniqueness are not guaranteed.There is repitition .
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am guessing that you can't change the implementation of the getRand() method. Nevertheless I will try to give you hints on how to construct the algorithm and then we can build on it the respective code.

So you need to get random numbers from 1 to 20 without repetition using the getRand() method. An implementation of it could be like this:

step 1: get a random number from getRand()
step 2: check that number if it is < 20. If not go to step 1
step 3: if number is < 20, check to see if it is unique or not
step 4: if it is unique, print it, if it is not, go to step 1

This is the algorithm that you should follow,which of course can be repeated for as many times as you want. For instance, if you want 3 random numbers, then you will repeat all steps 3 times. Try to implement your code with the above in mind and feel free come back for further questions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your method is liable to fill the array with the same value throughout.

You are going to have to get a piece of paper, a pencil and an eraser, the last bit of hardware being the most important. Write down what you are going to do to to achieve that. Also break it down into little steps. How are you going to get a number between 1 and 20, for example?
Don’t write any code until you know how it is supposed to work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant, Abhijit Ajansondkar’s method won’t work. I would not check that the number is < 20; I would do something different.

And welcome to the Ranch Abhijit Ajansondkar
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not check that the number is < 20; I would do something different


I am guessing that you are referring to step 2. Indeed, there are many ways to solve this, mine was just another approach.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote: . . . there are many ways to solve this, . . .
Agree. Most computing problems have many good solutions (and even more bad solutions).
Yes, I did mean step 2.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Ajansondkar wrote:Thanks Panagiotis Kalogeropoulos !
This is my code but it is producing the numbers < 20 but the number of elements and uniqueness are not guaranteed.There is repitition .[code=java]

There is also a basic problem with the "generate random number and see if it's a duplicate" method: As the choice of non-duplicate results diminishes, it takes longer and longer to find one. Obviously, the last value can simply be plugged in, but the second to last could take a significant amount of time to choose.

Here's an alternative method:
1. Plug all the possible values into an array.
2. Instead of using Random.nextInt() to "select" a value, use it to "shuffle" them. The easiest way to do this is via a series of 'swaps', but I leave it to you to work out how.
3. Once your "shuffle" is complete, your array should be in random order and it is guaranteed not to have any duplicates (unless you put them in there).

Winston
 
Abhijit Ajansondkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the constraint is the getRand() method . so you have don't have any other option except this method which produces a random integer between 1 to 100.
so please help me !
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Ajansondkar wrote:actually the constraint is the getRand() method . so you have don't have any other option except this method which produces a random integer between 1 to 100.

I just did. Substitute your method for the one I suggested. Or is your problem that you don't understand how to use it to generate results for n=20 and n=200?

so please help me !

So please EaseUp (←click). We're all volunteers here.

Winston

[Edit] BTW, that method is bad.
1. You don't need to create a new Random instance for each invocation.
2. Adding 1 is going to cause you problems.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! I never noticed you are creating a new random object every time you call that getRand method. You should’t do that.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Good grief! I never noticed you are creating a new random object every time you call that getRand method. You should’t do that.

Actually, the other problem of how to produce 200 distinct random values from an engine that only produces 100 is kind of fun - and not as obvious as you might think. Took me this morning's coffee to work it out.

Winston
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the other problem of how to produce 200 distinct random values from an engine that only produces 100 is kind of fun - and not as obvious as you might think. Took me this morning's coffee to work it out.


At first glance it seems intriguing, but actually it is not. I would like to show how I did it too so that we can compare different approaches and algorithms but I believe that this would be appropriate only after the topic starter, Abhijit Ajansondkar, shows us how he did it (so that we will not be accused of giving easy and painless code.... ).
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote:At first glance it seems intriguing, but actually it is not.

Well, I wouldn't classify it as up there with the Knight's Tour or the Philospher's problem, but it is one of those where you go "oh I know how...", and then realise about 10 seconds later that you didn't.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote: . . . (so that we will not be accused of giving easy and painless code.... ).
If you post code that I notice subtle errors in, I shall made d*mn sure to leave it. Mwaahaahaahaahaahaa!!
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote:
Yes, that is what they say after the copy and hand it in, and only later find the subtle errors!
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, that is what they say after the[y] copy and hand it in, and only later find the subtle errors!

Well, here's mine teach (assumes that getRand() returns a value between 0 and 99 inclusive):
(getRand() << 1) + (getRand() & 1);

I think OP has scarpered anyway.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like it. Of course, these subtle errors make one wish something else had been done properly in the first place.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic