Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:03:03+00:00 2026-06-04T10:03:03+00:00

Problem Statement : Execute Various Command randomly by matching its percentage. like execute CommandA

  • 0

Problem Statement: Execute Various Command randomly by matching its percentage.
like execute CommandA 50% of time and commandB 25% of the time and commandC 15% of time and etc etc and total percentage should be 100%.

My Question is- Execute CommandA A% of time, CommandB B%
of time, CommandC C% of time —– CommandZ Z% of time.
And total percentage should be 100% and at the end I can see
how much times each command is being executed and what is
the percentage of each command(means how many times each command is
being executed in terms of percentage) after total execution is complete.

Idea:-
Generate a random number between 1 and 100 and see if any of the percentage fall in the category

List<Double> comamndDistribution = new ArrayList<Double>();

    /* Commands to execute. Here I have Z command 
    *  and total percentage should be 100% (A+B+C+D+...+Z=100%)
    */
        comamndDistribution.add(A%); // command A
        comamndDistribution.add(B%); // command B
        comamndDistribution.add(C%); // command C
        comamndDistribution.add(D%); // command D
        comamndDistribution.add(E%); // command E
        -----------
        -----------
        comamndDistribution.add(Z%); // command Z

private Command getNextCommandToExecute() {

    for (int i=0; i < 10000; i++) {
       // generating a random number between 1 and 100
      int random = r.nextInt(100-1) + 1;

    /* My Question is- Execute CommandA A% of time, CommandB B% 
       of time, CommandC C% of time ----- Command Z Z% of time. 
       And total percentage should be 100% and at the end I can see
       how much times each command is being executed and what is 
       the percentage of each command(means how many times each command is 
       being executed in terms of percentage) after total execution is complete.
    */
        }
}

/* Get the next command to execute by maintaining the Percentage of 
    each command randomly*/
    Command nextCommand = getNextCommandToExecute();

Let me make it more clear- My Question is- Execute CommandA A% of time, CommandB B% of time, CommandC C% of time —– Command N N% of time by using Random Number. And total percentage should be 100%.

P.S: I think this question has been asked few times, but it is not the way I wanted it. So I thought to put it again as a question by posting my code what I did so far.

Update:- I have updated the question by removing the previous code that I wrote with another logic so that people can understand it more.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:03:04+00:00Added an answer on June 4, 2026 at 10:03 am

    I did it a similar way to Adam Liss, but it’s turned out more verbose.

    Again, I thought you can’t rely on the provided commands always adding up to 100%, so I’ve coped with that, but my way requires a method call to renormalise proportions (so a little more error prone).

    import java.util.*;
    
    /** 
      A basic 'Command' class for testing
    */
    class Command {
      private String id;
      public Command (String pId) {
        this.id = pId;
      }
      public void execute () {
        System.out.println ("Command: "+id);
      }
    }
    
    /** The class that does the random selection bit of magic */
    public class CommandDist {
    
      /** an internal helper class to manage proportions and the command */
      class Cmd {
        Command command;                 // the command that will get executed
        double assignedProportion;       // weight assigned when added
        double cumulativeProportion;     // recalculated later to between 0 and 1
    
        public Cmd (Command c, double proportion) {
          this.command = c;
          this.assignedProportion = proportion;
          this.cumulativeProportion = 0.0;
        }
      }
    
      // the list I'm using 
      private List<Cmd> commandDistribution = new ArrayList<Cmd>();
      private java.util.Random myRandom = new java.util.Random();
    
      void addCommand (Command command, double proportion) {
        commandDistribution.add ( new Cmd (command, proportion));
      }
    
      // ** MUST BE CALLED **, after adding all the commands, to normalise the proportions.
      // you could do this tidier by setting a flag in add, and checking it in
      // getNextCommandToExecute
      void normaliseProportion() {
        double total = 0;
        double cumulativeProp = 0;
        for (Cmd cmd: commandDistribution) {
           total += cmd.assignedProportion;
        }
        for (Cmd cmd: commandDistribution) {
           cumulativeProp += cmd.assignedProportion/total;
           cmd.cumulativeProportion = cumulativeProp;
        }
      }
    
    
      private Command getNextCommandToExecute () {
        double d = myRandom.nextDouble();
        for (Cmd cmd: commandDistribution) {
          if (d < cmd.cumulativeProportion) {
            return cmd.command;
          }
        }
        // theoretically, should not get here.  Never rely on theoretically.
        return commandDistribution.get(0).command;
    
      }
    
      public static void main (String [] args) {
        CommandDist cd = new CommandDist();
        Command c; 
    
        cd.addCommand (new Command ("A"), 50.0);
        cd.addCommand (new Command ("B"), 20.0);
        cd.addCommand (new Command ("C"), 15.0);
        cd.addCommand (new Command ("D"), 10.0);
    
        cd.normaliseProportion();
    
    
        for (int i = 0; i < 10000; i++) {
           c = cd.getNextCommandToExecute();
           c.execute();
        }
    
      }
    }
    

    The output looks like this:

    Command: C
    Command: A
    Command: C
    Command: A
    Command: D
    

    And generally distributes like this (different counts each run, of course).

    java CommandDist | sort | uniq -c
       5183 Command: A
       2151 Command: B
       1595 Command: C
       1071 Command: D
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem when trying to execute this update statement (below) using C#
I want to execute this statement $Data=$cash-$tax The problem is that the tax differs
Problem Statement: I'm creating a template for multi tiered complicated calculations in MS Excel
Problem statement: It is necessary for me to write a code, whether which before
Here is the problem statement: Calling a setter on the object should result in
I have a problem with a continue statement in my C# Foreach loop. I
I have a problem with the SQL statement detailed below. The query returns the
I have an big problem with an SQL Statement in Oracle. I want to
I got the problem that the if-statement doesn't work. After the first code line
I ran across a problem with a SQL statement today that I was able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.