# Categorical Random Number Generation with Fixed Parameters

**URL:** <https://discourse.mc-stan.org/t/categorical-random-number-generation-with-fixed-parameters/4923>\
**Category:** Modeling\
**Created:** [July 20, 2018, 12:41pm UTC](https://discourse.mc-stan.org/t/categorical-random-number-generation-with-fixed-parameters/4923 "2018-07-20T12:41:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrdevlar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mrdevlar/32/2812_2.png) [@mrdevlar](https://discourse.mc-stan.org/u/mrdevlar)\
**Post date:** [July 20, 2018, 12:41pm UTC](https://discourse.mc-stan.org/t/categorical-random-number-generation-with-fixed-parameters/4923/1 "2018-07-20T12:41:59Z")

</div>

Hi Everyone. I’m trying to figure out how to generate 100 random values from a categorical distribution with a fixed parameter set. However, I appear to be stuck and google and the manual have returned nothing I can piece together.

I am almost certain that it doesn’t appreciate my definition of the constant `simplex[3] theta = {0.5,0.3,0.2};` line, in fact it tells me so, but I do not know how to properly define it so this runs correctly. Any suggestions?

Stan Code

```
data {
 int N;
}
generated quantities {
  simplex[3] theta = {0.5,0.3,0.2};
  int y[N];
  for (i in 1:N){
    y[i] = categorical_rng( theta );
  }
}

```

R code

```
simu_data <- list(N = 100)
fit <- sampling(generative2, data=simu_data, iter=1000, algorithm = 'Fixed_param')

```

Any assistance is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 20, 2018, 12:59pm UTC](https://discourse.mc-stan.org/t/categorical-random-number-generation-with-fixed-parameters/4923/2 "2018-07-20T12:59:39Z")

</div>

> [@mrdevlar](#):
>
> it doesn’t appreciate my definition of the constant `simplex[3] theta = {0.5,0.3,0.2};`

You should’ve gotten an error message saying that you were trying to assign a `real[]` to a `vector`. These are different types. To create a vector, you need to create a row vector (`[x1, ..., xN]`) and transpose (with `'`):

```no-highlight
simplex[3] theta = [0.5, 0.3, 0.2]';

```

You can also generate the vector of counts by using the multinomial rng.

---

<div class="post-metadata">

**Author:** ![mrdevlar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mrdevlar/32/2812_2.png) [@mrdevlar](https://discourse.mc-stan.org/u/mrdevlar)\
**Post date:** [July 20, 2018, 1:01pm UTC](https://discourse.mc-stan.org/t/categorical-random-number-generation-with-fixed-parameters/4923/3 "2018-07-20T13:01:51Z")

</div>

It’s the transpose! That was what was missing.  
Awesome, thank you!
