import java.util.Random;
import java.awt.*;
import java.util.ArrayList;

public class Rand
{
    static int whichway = 2;

    public static void main(String[] args)
    {
	Rand r = new Rand();
	whichway=Integer.parseInt(args[4]);
	r.collect(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
	//r.printRes(0);
	r.plotRes(Integer.parseInt(args[3]));
    }

    private int[][] cou;
    private int[] tgt;
    private Random rr;
    private int expect = 10;
    public Rand()
    {
	rr = new Random();
    }
    public void collect(int size, int reps, int loops)
    {
	expect = reps/size;
	tgt = new int[size];
	cou = new int[size][size];

	for (int j=0; j<reps; j++)
	    {
		if (whichway==1)
		    {
			for (int k=0; k<size; k++)
			    tgt[k]=k;
			for (int z=0; z<loops; z++)
			    {
				for (int k=0; k<tgt.length; k++)
				    {
					int pl = (int)(Math.random()*tgt.length);
					int t = tgt[k];
					tgt[k]=tgt[pl];
					tgt[pl]=t;
				    }
			    }
		    }
		if (whichway==2)
		    {
			for (int k=0; k<size; k++)
			    tgt[k]=k;
			for (int z=0; z<loops; z++)
			    {
				for (int k=0; k<tgt.length; k++)
				    {
					int p1 = (int)(Math.random()*tgt.length);
					int p2 = (int)(Math.random()*tgt.length);
					int t = tgt[p2];
					tgt[p2]=tgt[p1];
					tgt[p1]=t;
				    }
			    }
		    }
		if (whichway==3)
		    {
			// sample without replacement from a secondary list
			ArrayList al=new ArrayList();
			for(int k=0; k<size; k++)
			    al.add(new Integer(k));
			for(int k=0; k<size; k++)
			    {
				int pl = (int)(Math.random()*al.size());
				tgt[k]=((Integer)al.get(pl)).intValue();
				al.remove(pl);
			    }
		    }
		if (whichway==4)
		    {
			// go halfway through
			for (int k=0; k<size; k++)
			    tgt[k]=k;
			for (int z=0; z<loops; z++)
			    {
				for (int k=0; k<tgt.length/2; k++)
				    {
					int pl = (int)(Math.random()*tgt.length);
					int t = tgt[k];
					tgt[k]=tgt[pl];
					tgt[pl]=t;
				    }
			    }
		    }
		if (whichway==5)
		    {
			// in-place smaple without replacement
			for (int k=0; k<size; k++)
			    tgt[k]=k;
			for (int z=0; z<loops; z++)
			    {
				for (int k=0; k<tgt.length; k++)
				    {
					int pl = k+(int)(Math.random()*(tgt.length-k));
					int t = tgt[k];
					tgt[k]=tgt[pl];
					tgt[pl]=t;
				    }
			    }
		    }

		for (int k=0; k<size; k++)
		    cou[k][tgt[k]]++;
	    }
    }
    public void printRes(int which)
    {
	for (int k=0; k<tgt.length; k++)
	    System.out.println(cou[which][k]);
    }
    int colorScale=1;
    public void plotRes(int cs)
    {
	colorScale=cs;
	Frame f = new Frame();
	f.setVisible(true);
	f.setSize(800,800);
	f.add(new Plotter());
    }

    private class Plotter extends Panel
    {
	public Plotter() {
	    setSize(800,800);
	}

	public void paint(Graphics g)
	{
	
	    //Graphics g = f.getGraphics();
	//long max = 0;
	//for (int k=0; k<tgt.length; k++)
	//    if (cou[which][k]>max) max=cou[which][k];
	int xsize=800/tgt.length;
	int ysize=800 / tgt.length;
	int yloc=10;
	for (int which=0; which<tgt.length; which++)
	    {
		int xloc = 10;
		for (int k=0; k<tgt.length; k++)
		    {
			int red=0;
			int blue=0;
			if (cou[which][k]>expect)
			    red=(int)(255 * ((cou[which][k]*1.0/expect)-1)*colorScale);
			else 
			    blue=(int)(255 * (1-(cou[which][k]*1.0/expect))*colorScale);
			if (red>255) red=255;
			if (blue>255) blue=255;
			g.setColor(new Color(red, 0, blue));
			//g.fillRect(xloc, yloc, xsize, (int)(cou[which][k] *1.0 / max * ysize));
			g.fillRect(xloc, yloc, xsize, ysize);
			xloc += xsize;
		    }
		yloc += ysize;
	    }

    }
    }
}
