This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Question:- The above Config File have some type of weighing to the Commands (execute CommandA 75% of the time, execute CommandB 15% of the time and execute Command C 10% of the time. Total weighing should add up to 100%.
Code that I wrote:- I wrote a multithreading program in which I am reading the files line by line but not sure how should I do the above question in which this much percentage of random calls go to CommandA, and this much percentage of random calls go to CommandB and same with CommandC.
If between 1 and 75; CommandA
76 and 89; CommandB
90 and 100; CommandC
Now, if your % vary based on input, you would need to set the upper and lower bounds accordingly.
Joined: Mar 23, 2010
So Suppose if I am generating random number between 1 and 100.
And if the number is between 1 and 75, I will execute CommandA, so that means it will execute CommandA 75% of the time?
And if the number is between 76 and 89, I will execute CommandB, so that means it will execute CommandB 15% of the time?
And same with CommandC?
Correct, In theory each number between 1 and 100 over an infinite time should be generated the same amount.
Since you're within the 75% for CommandA, it should fire 75% of the time ... or really close to that.
I assumed William had random integers on his mind and listed the numbers in inclusive convention (otherwise it would not work). I should have explicitly mentioned this when I started the nitpicking, since it is yet more common to state the boundaries as inclusive on the left and exclusive on the right, indeed.
Joined: Mar 23, 2010
How can I make this more configurable in the code as the percentage number will keep on changing in the config file, so based on the percentage in the config file how can I make this configurable for all the situations. But for this case I can hardcode the conditions but in general case how can I make this configurable. Any code snippet will be appreciated.
Raihan Jamal wrote:How can I make this more configurable in the code as the percentage number will keep on changing in the config file, so based on the percentage in the config file how can I make this configurable for all the situations. But for this case I can hardcode the conditions but in general case how can I make this configurable. Any code snippet will be appreciated.
Forget the code snippet. Your problem at the moment has nothing to do with Java and everything to do with defining your problem and solution outside of Java.
So, for simplicity's sake, lets say we'll continue dealing with ints, and that we'll simply want integer percentages that add up to 100. So you'll want an object that encapsulates the task and its percentage, yes? And then you'll want a container to store a bunch of these objects in, yes? And then you'll want some validation to make sure that their percentages add up to exactly 100, yes? (You wouldn't have to do it that way. It makes implementing the task chooser easier, but it puts more work on the code creating the tasks and assigning the percentages.)
So now you've got the following 3 objects:
Now, you generate a random integer in 1..100. Think about how you might map that integer to one of those tasks, completely independent of any Java code. (Turns out it's actually quite easy. )
Joined: Mar 23, 2010
Yes that's what my questions is, more specifically it is like-
Here A + B + C + . . . . + N should be 100%. Can anyone give me some sort of code snippet that will work on my config file. Any help will be appreciated.
Raihan Jamal wrote:Yes that's what my questions is, more specifically it is like-
Here A + B + C + . . . . + N should be 100%.
Yes, we know that.
Can anyone give me some sort of code snippet that will work on my config file. Any help will be appreciated.
This site isn't intended to give people solutions. The idea is to nudge toward finding your own solution.
Have you defined the class I suggested?
Have you figured out how to store some number of those objects?
What exactly have you done and what are you having trouble with?
If it's specifically the last step I mentioned--the mapping of a number to the right task, look again at what we have:
So if we have 52, which one does it belong to? How do you know that? How, exactly, in English and or pseudocode, did you figure out where it belongs? And, in particular, how does that 52 relate to Task1's X, Task2's Y, etc.?
And as another hint: If X is 50 and Y is 10 and Z is 40, then how do we relate those to the ranges 1..50, 51..60, and 61..100? (At this point it's really just basic arithmetic and logic. There's no programming. Writing the code will be the easy part once you get the steps figured out.)
Joined: Mar 23, 2010
I know how can I relate this if there are three tasks. For example if there are three tasks then I can have something like this-
1) stop writing java code. Start writing English sentences - or whatever natural language you prefer.
2) Pretend you are talking to a 5-year old child.
3) give them directions on how they they should choose what to do.
4) Revise these several times, making sure that any confusing/unclear point is...well...clarified.
come back here and post that, and we can move forward.
Raihan Jamal wrote:what I am most worried is that if we have suppose N tasks then how should I move.
The hints I've given you apply no matter how many tasks you have, and they don't require you to know the number of tasks ahead of time.
So, again, you have
And you have X1 = 1 and YN = 100.
And you can very easily determine Y1, X2, Y2, and all the others--you know that they're all sequential.
And then you get some number Z that's in 1..100.
How would you find which Task maps to Z? Think about how you would do this without Java.
And, finally, please BeForthrightWhenCrossPostingToOtherSites(⇐click). You've posted this question to at least 5 or 6 other sites. That can lead to people wasting their time repeating each others' answers.