r/dailyprogrammer – [Easy] Roll the Dies

This is my solution to a programming challenge found at reddit.com/r/dailyprogrammer. This problem can be found at Roll the Dies.

Josh


public class Easy {
	public static void main(String[] args) {	
		for (int i : rollDice("4d6")) {		// example data
			System.out.print(i + " ");
		}
	}
	
	private static int[] rollDice(String s) {		
		String diceData[] = s.split("d");
		int[] result = new int[Integer.parseInt(diceData[0])];		
		for (int i = 0; i < result.length; i++) {
			result[i] = (int)(Math.random() * Integer.parseInt(diceData[1]) + 1);
		}		
		return result;
	}
}

This entry was posted in Java, r/dailyprogrammer. Bookmark the permalink.

Leave a Reply