Rumor Simulation

<< Open House Password | OtherProjectsTrailIndex | Prisoner's Delemma >>

In this simulation, we pick the size of a party, and pick someone to start the rumor. Then whoever knows the rumor will actively spread the rumor to a randomly selected party member, and if that person has heard the rumor, the teller will stop spreading the rumor. If someone teller someone who is actively spreading the rumor, they too will stop spreading the rumor. If the person picked didn't know the rumor, then that person joins the active gossipers in spreading a rumor. Is it possible that people will stop spreading the rumor before everyone has heard the rumor? Run this simulation a few times and find out!

Gossiper.java


public class Gossiper 
{
    private String name;
    private boolean heardRumor;
    private boolean spreadingRumor;
	public Gossiper(String name){
		this.setName(name);
		setHeardRumor(false);
		setSpreadingRumor(false);
	}
	public boolean hasHeardRumor() {
		return heardRumor;
	}
	public void setHeardRumor(boolean heardRumor) {
		this.heardRumor = heardRumor;
	}
	public boolean isSpreadingRumor() {
		return spreadingRumor;
	}
	public void setSpreadingRumor(boolean spreadingRumor) {
		this.spreadingRumor = spreadingRumor;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString(){
		return name;
	}
}

RumorSimulation.java

import java.util.ArrayList;
import java.util.Scanner;


public class RumorSimulation 
{
     public static void main (String[] args){
    	  Scanner kb=new Scanner(System.in);
    	  System.out.print("How many at the party: ");
    	  int n=kb.nextInt();
    	  ArrayList<Gossiper> party = new ArrayList<Gossiper>();
    	  ArrayList<Gossiper> active = new ArrayList<Gossiper>();
    	  for (int i=0; i< n ; i++)
    		  party.add(new Gossiper("#"+i));
    	  Gossiper listener= party.get( pickSomeone(n) );
    	  listener.setHeardRumor(true);
    	  listener.setSpreadingRumor(true);
    	  active.add( listener );
    	  System.out.println("picked "+listener+" to start the rumor");
    	  int i=0;
    	  while (active.size()>0){
    		      Gossiper teller=active.get(i);
    			  listener= party.get( pickSomeone(n) );
    			  System.out.println("("+teller+" tells "+listener+")");
        		  if (listener.hasHeardRumor()){
        			  teller.setSpreadingRumor(false);
        			  active.remove(teller);
        			  i--;
        			  if (listener.isSpreadingRumor()){
        				  listener.setSpreadingRumor(false);
        				  active.remove(listener);
        				  i--;
        			  }

        		  } else {
        			  listener.setHeardRumor(true);
        			  listener.setSpreadingRumor(true);
        			  active.add(listener);
        			  i++;
        		  }
        		  System.out.print("There are "+active.size()+" actively speading the rumor: ");
    	          for (Gossiper x:active)
    	        	        System.out.print(x.getName()+" ");
    	  }

    	  System.out.println("Those who haven't heard the rumor:");
    	  for (Gossiper x:party)
    		  if (!x.hasHeardRumor())
    			  System.out.print(x.getName()+", ");
     }

     public static int pickSomeone(int max){
    	 	return (int)(max*Math.random());
     }
}

  1. Can you see that someone can choose to tell oneself? Maybe this situation can simulate someone who refuses to gossip, but maybe you can improve the code so this won't happen!
  2. Can you add to the code so it counts how many haven't heard the rumor?
  3. Can you change the code so it runs the simulation 10 times and computes the average number of people who never heard the rumor?