@Override
	public Object evaluate(DeferredObject[] arguments) throws HiveException {
		if (random == null){
			random = new MersenneTwister();
		}
		for (int i=0;i<arguments.length;i++){
			if (arguments[i].get()==null)
				return null;
		}
		List<IntWritable> results = new ArrayList<IntWritable>();
		PrimitiveCategory compareCategory = 
				PrimitiveCategory.INT;
		int N = ((IntObjectInspector)argumentOI[0]).get(arguments[0].get());
		double p = ((DoubleObjectInspector)argumentOI[1]).get(arguments[1].get());
		
		int samples = 1;
		if (providedSamplesArgument){
			samples = ((IntObjectInspector)argumentOI[2]).get(arguments[2].get());
		}
		BinomialDistribution dist = new BinomialDistributionImpl(N,p);

		
		for (int i = 0; i < samples; i++){
			IntWritable value = new IntWritable();
			try {
				// Must add one for some goofy reason
				// either their rounding down even though documentation suggests otherwise
				// or their encoding outcomes weirdly
				int val = dist.inverseCumulativeProbability(random.nextDouble())+1;
				if (val<0)val = 0;
				value.set(val);
			} catch (MathException e) {
				value.set(-1);
				e.printStackTrace();
			}
			results.add(value);
		}
		if (providedSamplesArgument)
			return results;

		return results.get(0);
	}