# Any alternative to randomSwitch() to distribute requests amongst multiple scenarios/chainbuilders?

**URL:** <https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420>\
**Category:** Gatling (Open-Source)\
**Created:** [November 2, 2023, 2:54pm UTC](https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420 "2023-11-02T14:54:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raghvendra](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/raghvendra/32/1170_2.png) [@Raghvendra](https://community.gatling.io/u/Raghvendra)\
**Post date:** [November 2, 2023, 2:54pm UTC](https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420/1 "2023-11-02T14:54:20Z")

</div>

Hello Folks,

I am trying to identify an alternative to randomSwitch for distributing load amongst different chainbuilders based on percentage.  
The problem that I see with randomSwitch is it doesn’t exactly follow the weights defined example 5 and 95 split. Sometimes i’ll get 3% and 97% others i’ll get 1% and 99% and so on… but doesn’t guarantee 5% and 95%.

I tried something like this (sudo code) but it didn’t even put the groups in report:

```auto
ScenarioBuilder fixedFlow = scenario ("Scenario for Fixed Flow")
    .exec(mb.generateTraceId)
            .group("Fixed").on(exec(session -> {
                int nextCount = 0;
                nextCount++;
                if (nextCount % 100 < 2) {
                    group("Users with large fixed")
                            .on(feed(mfixedFeeder.circular()))
                            .exec(new LDSimulation().getMLDJourney());
                } else {
                    group("Users with Less fixed")
                            .on(feed(ldFeeder.circular()))
                            .exec(new LDSimulation().getMLDJourney());
                }
                return session;
            }))

```

When I write this, I see that it gets executed (may be as if I put print statements inside if else, they are getting printed and hence I am thinking execs are also getting executed) but it does not appear int he report.

Hence could someone please suggest an alternative to randomSwitch which guarantees weights or could someone help me correct my code so that the chainbuilders getting executed inside exec inside session gets included inthe report. Thanks for your help in advance.

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [November 2, 2023, 5:21pm UTC](https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420/2 "2023-11-02T17:21:54Z")

</div>

Hi @Raghvendra,

The code you provide cannot work. As stated in the first alert box of the documentation: [Gatling - Concepts](https://gatling.io/docs/gatling/reference/current/core/concepts/)

> The components of this DSL are mere **definitions** of the desired effect. They don’t generate the desired effect where you invoke them in your own code. Only when chained with other components so they are ultimately passed to the [setUp](https://gatling.io/docs/gatling/reference/current/core/simulation/#setup), can the Gatling engine interpret them and produce the desired effect.

My feelings about your use case is that they are different category of users, so they should be described by different scenario.

```java
ScenarioBuilder createFixedFlow(String scnName, Feeder feeder) {
  return scenario ("Scenario for " + scnName + " Fixed Flow")
    .exec(mb.generateTraceId)
            .group("Fixed").on(
                    group("Users with " + scnName + " fixed")
                            .on(feed(feeder))
                            .exec(new LDSimulation().getMLDJourney());
            );
}

ScenarioBuilder mfixedScn = createFixedFlow("large", mfixedFeeder.circular());
ScenarioBuilder ldFixedScn = createFixedFlow("Less", ldFeeder.circular());

```

Then you can inject the amount of user you want in the setUp call.

Does that help?

Cheers!

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 5, 2023, 10:04am UTC](https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420/3 "2023-11-05T10:04:03Z")

</div>

That being said, you can achieve the desired algorithm:

- use a global atomic counter for `nextCount `
- compute the distribution condition in an exec(function) block
- use `doSwitch` based on this condition

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [December 5, 2023, 10:05am UTC](https://community.gatling.io/t/any-alternative-to-randomswitch-to-distribute-requests-amongst-multiple-scenarios-chainbuilders/8420/4 "2023-12-05T10:05:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
