# Custom Distribution of Random Number

**URL:** <https://discourse.processing.org/t/custom-distribution-of-random-number/13628>\
**Category:** Beginners\
**Created:** [August 26, 2019, 6:03pm UTC](https://discourse.processing.org/t/custom-distribution-of-random-number/13628 "2019-08-26T18:03:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tylerowens](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@tylerowens](https://discourse.processing.org/u/tylerowens)\
**Post date:** [August 26, 2019, 6:03pm UTC](https://discourse.processing.org/t/custom-distribution-of-random-number/13628/1 "2019-08-26T18:03:05Z")

</div>

Hello all, I am currently in the introduction chapter of the nature of code. I am making a program that generates a random number by comparing two random numbers. I am able to make sense of everything besides what randomCount[x] is generating as i have not set any of the arrays index values to anything.

Any help explaining how this is working would be appreciated. Thank you.

 ![24%20AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/43bfe9c43ee3b880256f8c0cc6941c74fe31968b.png)

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [August 26, 2019, 7:00pm UTC](https://discourse.processing.org/t/custom-distribution-of-random-number/13628/2 "2019-08-26T19:00:52Z")

</div>

Evening Tyler, welcome to the forum. For future post I recommend using the preformatted text button (the one shaped like **\</\>** ) for sharing code. Makes it a bit easier for us to copy-paste and play around with it 🙂

If I understand your code correctly, int index is the **array number** (0 - 19), and countArray[x] counts **how often** that index has been randomly ‘rolled’. You start out with a completely empty array with 20 slots, but each time your sketch goes through draw it adds +1 to one of these 20 slots. How often each slot is rolled is visually shown through graphs because of this line:

```auto
rect(x * w, height - randomCount[x], w - 1, randomCount[x]);

```

it picks the bottom of the screen (_height_) and subtracts the amount of times that array slot is rolled (_-randomCount[x]_). The utmost left slot for instance, which is randomCount[0], will often remain 0 for a longer time than the other slots.

Does that clear things up for you?

---

<div class="post-metadata">

**Author:** ![tylerowens](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@tylerowens](https://discourse.processing.org/u/tylerowens)\
**Post date:** [August 26, 2019, 8:15pm UTC](https://discourse.processing.org/t/custom-distribution-of-random-number/13628/3 "2019-08-26T20:15:54Z")

</div>

Thank you, i was trying to figure out how to use the reformatted button.

Ok this is starting to make sense, through changing the size of the array i did realize that the rectangles were visualizing the array length.

One more thing that i could use a little more clarifying on, The line:  
`int index = int (acceptreject() * randomCount.length); randomCount[index]++;`

is taking the generated number from

```auto
float acceptreject(){
  boolean foundone = false;
  while (!foundone){
    float r1 = (float) random(1);
    float r2 = (float) random(1);
    if (r2 < r1){
      foundone = true;
      return r1;
    }
  }
  return 0;
}

```

which will generate a number between 0-19 and that is where the ‘rolling’ comes from, and in turn makes one of the rectangles increase in height?

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [August 26, 2019, 9:14pm UTC](https://discourse.processing.org/t/custom-distribution-of-random-number/13628/4 "2019-08-26T21:14:43Z")

</div>

The while loop within **float acceptreject()** keeps rolling/randomising r1 and r2 until r1 is bigger. This value will be returned and multiplied by **randomCount.length** (which is in this case 20). After the multiplication the number will be rounded to a whole number because it’s between **int( … )**.

This rounded number will be used to pick the right slot for **randomCount[index]**. So if the calculations of index results in 15, it means that array slot #15 gets selected. By adding _++_ right after it it means that the value within that slot increases by one.

Since the returned value will be between 0 and 1 (you can test this by adding _println(r1)_ right after _foundone = true;_) and gets multiplied by the length of the array, the outcome of index won’t exceed the slot numbers of your array.
