Random simulations of the distribution of genders in a classroom For my AP statistic class, we had to design an experiment (usually a survey of what brand of soda people prefer), so me being an overachiever decided to study how boys and girls distribute themselves among other genders. For instance, girls will sit near each other normally.
My way of doing this would be to find the average percentage of people guys sit around that are the same gender for both genders independently. I'd expect to see these numbers be $50$% if they sat randomly, but I'm willing to bet they're around $75$%-$85$%.
How can I produce random simulations of this test? It's a requirement for the assignment.
 A: For a successful simulation you need three things. First, a well-defined measure of what is important, here 'sitting close'. Second, a way to way to model
what you mean by random behavior, here 'unbiased seating, not influenced
by gender'.  Third, an automated way of computing the measure, so that
you can run many iterations on a computer without having to do the evaluation
by hand.
To give a simple illustration, suppose we consider positions of boys and
girls along a row of a dozen seats. One measure of closeness or 'clumpiness'
of seating might be runs. 
Runs as a measure: If there are 6 boys and 6 girls, then the
smallest number of runs is two: BBBBBBGGGGGG or GGGGGGBBBBBB, for maximum
clumpiness. And the largest number of runs is 12 BGBGBGBGBGBG or
GBGBGBGBGBGB for minimum clumpiness. 
Random scrambling: One possibility would be to have equal numbers of boys and girls,
as above. On a computer it is easy to scramble 6 boys and 6 girls at random.
In order to use numbers in stead of letters, use 1's for girls and 2's for boys.
In R statistical software the sample function will easily make random
arrangements or permutations: We start with a vector kids containing six
1's and six 2's. Then we repeatedly permute them using sample.
kids = rep(1:2, each=6);  kids
## 1 1 1 1 1 1 2 2 2 2 2 2
perm = sample(kids, 12); perm
## 1 1 2 2 2 1 2 1 2 1 2 1          # 1st random permutation     
perm = sample(kids, 12); perm
## 1 2 2 2 2 1 2 1 1 1 1 2          # 2nd random permutation
perm = sample(kids, 12); perm
## 1 1 2 1 2 2 1 1 2 2 2 1          # 3rd
perm = sample(kids, 12); perm
## 1 2 2 1 1 2 2 1 1 2 2 1          # etc.
perm = sample(kids, 12); perm
## 1 2 1 2 2 2 1 2 2 1 1 1

Automatic counting of runs: In R the function rle (for Run Length Encoding)
will find the runs in a permutation and count the length of each run. Here is
an example for one permutation.
perm = sample(kids, 12); perm
## 1 1 1 2 2 1 1 2 1 2 2 2
rle(perm)
## Run Length Encoding
## lengths: int [1:6] 3 2 2 1 1 3
## values : int [1:6] 1 2 1 2 1 2

The translation is that we have six runs in this permutation:
The first run consists of three 1's, the second run consists of two 2's,
the third of two 1's, and so on.
We can capture the lengths and values as follows:
info = rle(perm)
info$lengths
## 3 2 2 1 1 3
length(info$lengths)
## 6                     # number of runs
max(info$lengths)
## 3                     # length of longest run
mean(info$lengths)
## 2                     # average run length

A simulation: We can put this altogether to find the 'typical behavior'
of runs of six boys and six girls sitting at random in a row of 12 seats.
At the beginning we make vectors nr and mx to receive the results
of 10,000 scramblings of 12 kids. The actual numbers are put into these
vectors one at a time as the program goes through the 'for' loop.
m = 10000;  nr = mx = numeric(m);  kids = rep(1:2, each=6)
for (i in 1:m)  {
   perm = sample(kids, 12)
   info = rle(perm)
   nr[i] = length(info$lengths)
   mx[i] = max(info$lengths)    }
mean(nr);  mean(mx)
## 7.0126
## 3.1963

So when six boys and six girls sit at random in a row of 12 seats
without regard to gender, there will be about 7 runs, and the longest
run will be about 3.   
We can make histograms of the simulated numbers
and maximum lengths. It is probably easiest to interpret run lengths. It seems that if there are fewer than 3 or 4 runs
when real groups of 6 girls and 6 boys sit in a row, then there may be
clumping together of genders. And if there are more than 10 or 11 runs,
then boys and girls may be tending to 'pair up'.

Histogram code, in case you want it:
par(mfrow=c(1,2))
 hist(nr, prob=T, br=(min(nr):(max(nr)+1))-.5, col="skyblue2", main="Dist'n of Numbers of Runs")
 hist(mx, prob=T, br=(min(mx):(max(mx)+1))-.5, col="skyblue2", main="Dist'n of Longest Runs")
par(mfrow=c(1,1))

Notes: (1) There is a lot of theoretical information about the distributions
of numbers of runs and run lengths in intermediate-level probability and mathematical statistics books. You might get some worthwhile information from Wikipedia and university sites. 
(2) A larger number m of iterations will give more precise results. 
The program runs fast enough on today's laptops that more extensive
simulations are feasible.
(3) R is free software available at www.r-project.org.
A: There are many approaches you can take, I'll describe a straightforward one here. 
First, it's important to note that your simulation results will be affected by (1) the geometry of the classroom and (2) the number of boys vs. girls. So if you are eventually going to compare it to real data, you want to match the simulation accordingly.
Let's define some parameters:


*

*$R$ = number of rows

*$C$ = number of columns

*$B$ = number of boys in class

*$G$ = number of girls in class

*$N$ = total number of students


Hence $RC >= N$ (some seats may be empty), and $B+G = N$.
Next, you want to decide what you mean "sit around". Specifically, do you want to look at just students who sit next to each other, in front or behind, who sit diagonally, etc? 
In the diagram below I only consider up'down'left'right as neighbors for $R=4$, $C=6$, $B=15$, $G=9$, $N=24$.
For the simulation, you want to first assign a boy or a girl randomly to each desk. After that's complete, you then want to loop over each student, counting the number of neighbors having the same gender. Taking an average over all students (being sure to take into account that students can have either 2,3, or 4 neighbors) will give you the percentage you want. You then want to run the simulation many times to get an accurate distribution.
Hope this helps, I'm happy to continue if some of the steps are still confusing.

