Doug's Random Sampling Applet: Source Code

Doug's Random Sampling Applet is "open source."  That means that you get to look at the source code.  It also means that Doug is now "cool" in the "geek" "community."  Yesterday I was the merest of mere math professors.  Today I am an open source pioneer, a programming outlaw who makes his own rules and doesn't even necessarily follow them.  You don't want to get mixed up with me; I'm dangerous.

import java.awt.*;
import java.util.Random;
public class andrea extends java.applet.Applet {
int numbers[]=new int[1001];
int rand;
int sampsize=0;
TextField sample,from,to;
Checkbox alph,nalph,rep,nrep;
Button go;
public void init(){
	int i;
	sample=new TextField("10",3);
	from=new TextField("20",3);	
	to=new TextField("100",3);
	CheckboxGroup cbg=new CheckboxGroup();
	rep=new Checkbox("Allow Repeats",cbg,false);
	nrep=new Checkbox("Don't Allow Repeats",cbg,true);
	CheckboxGroup cbg2=new CheckboxGroup();
	alph=new Checkbox("Put them in order",cbg2,true);
	nalph=new Checkbox("Don't put them in order",cbg2,false);
	go=new Button("Go!");	
	add(new Label("The set is from"));
	add(from);
	add(new Label("To"));
	add(to);
	add(new Label("You are choosing a set of size"));
	add(sample);
	add(rep);
	add(nrep);
	add(alph);
	add(nalph);
	add(go);
}
public void withoutreplace(int lower,int upper,int range){
	int i,w;
	Random generator=new Random();
	for(i=1;i<=range;i++){
		numbers[i]=i+lower-1;
	}
	for(i=1;i<=range;i++){
		rand=generator.nextInt();
		rand=Math.abs(rand%(range-(i-1)))+i;
		w=numbers[i];
		numbers[i]=numbers[rand];
		numbers[rand]=w;
	}
}
public void withreplace(int lower,int range,int sampsize){
	int i;
	Random generator=new Random();
	for(i=1;i<=sampsize;i++){
		rand=generator.nextInt();
		rand=Math.abs(rand%(range))+lower;
		numbers[i]=rand;
	}
}
public void ordering(int limit){
	int i,j,w;
	for(i=1;i<=limit-1;i++){
		for(j=i+1;j<=limit;j++){
			if(numbers[i]>numbers[j]){
				w=numbers[i];
				numbers[i]=numbers[j];
				numbers[j]=w;
			}
		}
	}
}
public boolean action(Event evt, Object arg) {
	if(evt.target instanceof Button){
		sampsize=Integer.parseInt(sample.getText());	
		int lower=Integer.parseInt(from.getText());
		int upper=Integer.parseInt(to.getText());	
		int range=upper-lower+1;
		if(nrep.getState()==true) {
			withoutreplace(lower,upper,range);
		}
		else{
			withreplace(lower,range,sampsize);
		}
		if(alph.getState()==true){
			ordering(sampsize);
		}
		repaint();
		return true;
	}
	else
		return false;
}
public void paint(Graphics g) {
	int i;
	int x=0,y=go.location().y+60;
	String temp;
	Font f = new Font("TimesRoman",Font.PLAIN,16);
	for(i=1;i<=sampsize;i++)
	{
		if(x>size().width-60){
			x=0;
			y+=30;
		}
		temp=" "+numbers[i];
		g.setFont(f);
		g.drawString(temp,x,y);
		x+=30;
	}
}
}


widget.gif (1386 bytes) Back to Doug's Random Sampling Applet
widget.gif (1386 bytes) Back to Main

Doug Code 6/2000:   imk lomg o xim’b topp cwifb gncjfcbk phiir op lk skkromg is xchomg fv kyknu tinmomg cme xopqomg o xcp ekcj. -eifg pqcx