Raihan Jamal

Ranch Hand

Posts: 86

posted 5 years ago

This is my Config File(Test.txt)

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%.

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.

**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.

Raihan Jamal

Ranch Hand

Posts: 86

posted 5 years ago

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?

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?

posted 5 years ago

Just a small note, it probably should be:

If between 1 and 75; CommandA

76 and

assuming the random generator actually returns numbers between 1 and 100 inclusive. I'd say having the range of random numbers to be 0 to 99 inclusive would be more natural.

If between 1 and 75; CommandA

76 and

**90**; CommandB**91**and 100; CommandC,assuming the random generator actually returns numbers between 1 and 100 inclusive. I'd say having the range of random numbers to be 0 to 99 inclusive would be more natural.

posted 5 years ago

I'm not sure that is right. You seem to be saying that if they want 75%, you need to go from 1 to 75. So if they want a command 1% of the time, should they go between 1 and...1?

similarly, values from 91-92 represent 1%, 91-93 represent 2%, 91-94 represent 3%...and 91-100 represent 9%.

are we generating ints or floats?

Martin Vajsar wrote:Just a small note, it probably should be:

If between 1 and 75; CommandA

76 and90; CommandB

91and 100; CommandC,

assuming the random generator actually returns numbers between 1 and 100 inclusive. I'd say having the range of random numbers to be 0 to 99 inclusive would be more natural.

I'm not sure that is right. You seem to be saying that if they want 75%, you need to go from 1 to 75. So if they want a command 1% of the time, should they go between 1 and...1?

similarly, values from 91-92 represent 1%, 91-93 represent 2%, 91-94 represent 3%...and 91-100 represent 9%.

are we generating ints or floats?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 5 years ago

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.
Raihan Jamal

Ranch Hand

Posts: 86

posted 5 years ago

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.

posted 5 years ago

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

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,

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. )

Raihan Jamal

Ranch Hand

Posts: 86

posted 5 years ago

Yes, we know that.

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,

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.)

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.)

Raihan Jamal

Ranch Hand

Posts: 86

posted 5 years ago

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-

if(R>1 && R<percentageCalls.get(0)) {

} else if(R>(percentageCalls.get(0) + 1) && R<(percentageCalls.get(0) + percentageCalls.get(1))) {

} else if(R>(percentageCalls.get(0) + percentageCalls.get(1)) && R<(percentageCalls.get(0) + percentageCalls.get(1) + percentageCalls.get(2))) {

}

Where R is random number and percentageCalls is the ArrayList of Integer where I have stored all the percentage. But what I am most worried is that if we have suppose N tasks then how should I move.

if(R>1 && R<percentageCalls.get(0)) {

} else if(R>(percentageCalls.get(0) + 1) && R<(percentageCalls.get(0) + percentageCalls.get(1))) {

} else if(R>(percentageCalls.get(0) + percentageCalls.get(1)) && R<(percentageCalls.get(0) + percentageCalls.get(1) + percentageCalls.get(2))) {

}

Where R is random number and percentageCalls is the ArrayList of Integer where I have stored all the percentage. But what I am most worried is that if we have suppose N tasks then how should I move.

posted 5 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

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.

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.

posted 5 years ago

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

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.

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.

posted 5 years ago

Okay, so you know that you need to look at the running total of percentages for each task: 0, then 0,1, then 0,1,2, etc. Good. That's the heart of the whole thing.

Now you're basically asking this question:

You're saying, "I know how to print 1, and how to print 1, 2, and how to print 1, 2, 3, but how do I print 1..N?"

"How do I print up to N if I don't know N ahead of time, and without writing N explicit print statements"?

This is

Raihan Jamal wrote:

} else if(R>(percentageCalls.get(0) + 1) && R<(percentageCalls.get(0) + percentageCalls.get(1))) {

} else if(R>(percentageCalls.get(0) + percentageCalls.get(1)) && R<(percentageCalls.get(0) + percentageCalls.get(1) + percentageCalls.get(2))) {

Okay, so you know that you need to look at the running total of percentages for each task: 0, then 0,1, then 0,1,2, etc. Good. That's the heart of the whole thing.

Now you're basically asking this question:

You're saying, "I know how to print 1, and how to print 1, 2, and how to print 1, 2, 3, but how do I print 1..N?"

"How do I print up to N if I don't know N ahead of time, and without writing N explicit print statements"?

This is

**exactly**what you are asking here.