MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Populate an array randomly with some constraints

Asked by Clifford Shelton on 4 Aug 2012

This has had me stumped all day. Any help is appreciated. I'm trying to do the following with an array:

1.) randomly populate an array called 'astrosRuns' with 8133 values.

2.) the possible values to populate the 'astrosRuns' array with range in values (0:16)

3.) I want each possible value of that range to have a specific probability of occurring.

4.) I also want to establish the constraint that every group of 10 values within the 'astrosRuns' array has an average value of 4.

I'm a beginner so I've developed some code (which is probably very inefficent) to attempt to tackle goals 1,2, and 3 from above. I'm totally clueless how to do 4.

What i've done so far is construct a for loop for the 8133 steps and made a bunch of if else if statements for the probabilities of each value between the range of (0:16) occurring.

How would I then also have the program adhere to another constraint (or bias) that every unique group of 10 values will combine to have an average value of 4 (or in essence a sum of 40)

So..the first 10 values could be (8,5,7,4,0,0,1,4,5,6) which has an avearge of 4. But the program will be smart enough to generate the 8133 values within the array to adhere to the given probabilities 'j' AND the 10 value average rule of 4.

here's my code so far:

```% This for loop begins calculating the probabilities for the occurence of a given score.  I set this up as a range between values that were taken from the histogram.  I had to do this manually since I don't know an easier way yet.
for i=1:8133;
%    Let j = the probability for the score value
%     Let k = the probability that the adjacent values will be equal
```
```j =rand;
k =rand;
```
```if 0<=k<=.0063
astrosRuns(i+1)=astrosRuns(i);
```
```elseif 0<=j<=.0687
astrosRuns(i+1)=0;
```
```elseif .0688<=j<=.1880
astrosRuns(i+1)=1;
```
```elseif .1881<=j<=.3286
astrosRuns(i+1)=2;
```
```elseif .3287<=j<=.4801
astrosRuns(i+1)=3;
```
```elseif .4802<=j<=.6110
astrosRuns(i+1)=4;
```
```elseif .06111<=j<=.7223
astrosRuns(i+1)=5;
```
```elseif .7224<=j<=.8050
astrosRuns(i+1)=6;
```
```elseif .8051<=j<=.8696
astrosRuns(i+1)=7;
```
```elseif .8697<=j<=.9156
astrosRuns(i+1)=8;
```
```elseif .9157<=j<=.9455
astrosRuns(i+1)=9;
```
```elseif .9456<=j<=.9671
astrosRuns(i+1)=10;
```
```elseif .9672<=j<=.9797
astrosRuns(i+1)=11;
```
```elseif .9798<=j<=.9874
astrosRuns(i+1)=12;
```
```elseif .9875<=j<=.9931
astrosRuns(i+1)=13;
```
```elseif .9932<=j<=.9962
astrosRuns(i+1)=14;
```
```elseif .9963<=j<=.9984
astrosRuns(i+1)=15;
```
```else
.9985<=j<=1;
astrosRuns(i+1)=16;
end
end
```

ANY help is most appreciated. Please keep in mind that I'm a beginner...so if you could please explain any answers with a TON of detail..I just might understand. Please answer as if I'm the dumbest programmer you've ever met!

thanks!

Clifford Shelton on 4 Aug 2012

wow. I honestly don't mean to frustrate everyone. Some bits and pieces of suggestions were given...however it seemed that the meat of my question (how do I BOTH keep a moving average of 4 for each group of 10 AND keep the probabiliitess for 'j' the same) wasn't answered.

Maybe it was...but as I've tried to state repeatedly...I'm a beginner!!!! So small snippets of code with abbreviations isn't going to sink in with me. Unfortunately, I'd need very detailed responses to really get it.

So...I did notice the suggestion of putting the data set into 'bins' of 10.

Would anyone be kind enough to explain to me exactly how that is done...then maybe I can tackle the moving average part. And let's just change the size of the data set to 8130 for ease.

Once again...sorry for being a beginner!! I really appreciate the help from everyone: Sven, Oleg, Image Analyst Woski etc....

Sven on 4 Aug 2012

Ok, so here's my suggestion: when you describe that the meat of your problem is "how to BOTH keep a moving average of 4 AND keep the probabilities the same", it is clear that you are trying to do too much at once, especially for a beginner (and there's nothing wrong with being a beginner).

I haven't even seen you take one of the (multiple) suggestions for "keep a moving average of 4", and show that "I've got this far and my code works, but I'd really like to do something slightly different".

So, here's your task: show that you've got the first of your requirements under control. Show us that you understood the responses and can make a matrix with 813 sets of 10 numbers each averaging 4.

If you didn't understand the responses, that's ok! Just ask about that before making another question - it won't make it any easier to understand if you only show your original non-working code and just make the question more complex.

Image Analyst on 5 Aug 2012

I don't think you can require that a window of 10 scores can average 4 and at the same time have each of the 10 scores be a random integer between 1 and 16 with a certain probability for each number. You can easily specify a certain probability for 16 numbers by getting a random number between 0- and 1 and chopping that interval into 16 chunks of different widths:

```randomNumber = rand(1);
if randomNumber  < 0.06
runs = 1;
elseif randomNumber  < 0.1235
runs = 2;
```

and so on. But the average will not necessarily be an integer and it's even less likely to be 4. In fact you'd have to force the last, 10th, element to be a certain number to get the mean to be 4 and that number almost certainly won't be an integer, and it certainly won't be random at all. It would be a predetermined floating point number - it has to be in order to get the mean of 4.

## Products

No products are associated with this question.

Answer by Daniel on 4 Aug 2012

I am going to give you the same answer I gave you before. The line

```elseif .0688<=j<=.1880
```

does not do what you think it does.