Commit bf9ffad1 authored by samuel.boulton's avatar samuel.boulton

1 left

parent c4fffa49
...@@ -74,11 +74,10 @@ public class Program { ...@@ -74,11 +74,10 @@ public class Program {
// DONE AND PASSED TEST // DONE AND PASSED TEST
int[] resultsArray = new int[binaryArray.length]; int[] resultsArray = new int[binaryArray.length];
// for loop to go around the binary array
for(int i = 0; i<binaryArray.length; i++) for(int i = 0; i<binaryArray.length; i++)
{ {
// use the 'ConvertASingleWordToInt' method to work out each character
// then add each character to the resultsArray
resultsArray[i] = ConvertASingleWordToInt(binaryArray[i]); resultsArray[i] = ConvertASingleWordToInt(binaryArray[i]);
} }
...@@ -90,20 +89,19 @@ public class Program { ...@@ -90,20 +89,19 @@ public class Program {
int result = 0; int result = 0;
int binCM = 1; int binCM = 1;
// making the characters from said 'someWord'
char[] binChar = someWord.toCharArray(); char[] binChar = someWord.toCharArray();
// looping through the characters
for (int i = 0; i < binChar.length; i++) for (int i = 0; i < binChar.length; i++)
{ {
// making the program go through the characters backwards to get the correct result
int binIndex = binChar.length - i - 1; int binIndex = binChar.length - i - 1;
// if statement seeing if the character is a 1
if (binChar[binIndex] == '1') if (binChar[binIndex] == '1')
{ {
// if it is a 1 then do this calcualtion
result = result + binCM; result = result + binCM;
} }
// each time the loop goes round binCM doubles
binCM = binCM * 2; binCM = binCM * 2;
} }
...@@ -115,11 +113,9 @@ public class Program { ...@@ -115,11 +113,9 @@ public class Program {
public static int[] ConvertHexToInt(String[] hexArray) { public static int[] ConvertHexToInt(String[] hexArray) {
// DONE AND PASSED TEST // DONE AND PASSED TEST
int[] resultsArray = new int[hexArray.length]; int[] resultsArray = new int[hexArray.length];
// for loop to go around the hex array
for(int i = 0; i<hexArray.length; i++) { for(int i = 0; i<hexArray.length; i++) {
// use the 'ConvertHexToInt' method to work out each character
// then add each character to the resultsArray
resultsArray[i] = ConvertHexToInt(hexArray[i]); resultsArray[i] = ConvertHexToInt(hexArray[i]);
} }
...@@ -133,12 +129,11 @@ public class Program { ...@@ -133,12 +129,11 @@ public class Program {
{ {
int result = 0; int result = 0;
int hexCM = 1; int hexCM = 1;
// making the characters from said 'hexWord'
char[] hexChar = hexWord.toCharArray(); char[] hexChar = hexWord.toCharArray();
// looping through the characters
for(int i = 0; i < hexChar.length; i++) for(int i = 0; i < hexChar.length; i++)
{ {
// if the character is a certain character then do this calculation
int hexIndex = hexChar.length - i - 1; int hexIndex = hexChar.length - i - 1;
if(hexChar[hexIndex] == '1') {result = result + (hexCM * 1);} if(hexChar[hexIndex] == '1') {result = result + (hexCM * 1);}
if(hexChar[hexIndex] == '2') {result = result + (hexCM * 2);} if(hexChar[hexIndex] == '2') {result = result + (hexCM * 2);}
...@@ -577,40 +572,83 @@ public static int Range (int[] input) { ...@@ -577,40 +572,83 @@ public static int Range (int[] input) {
public static double[] GenerateRandomDistributionForSpecificDie(int iterations) { public static double[] GenerateRandomDistributionForSpecificDie(int iterations) {
// DONE AND PASSED TEST
double[] resultsArray = new double[5121]; double[] resultsArray = new double[5121];
Random rng = new Random(1234); Random rng = new Random(1234);
for(int i = 0; i<iterations; i++) for(int i = 0; i<iterations; i++)
{ {
int numIGot = 0;
numIGot = RollASingle256SidedDie20Times(rng);
resultsArray[numIGot] = resultsArray[numIGot] + 1;
} }
for(int j = 0; j < resultsArray.length; j++)
{
resultsArray[j] = resultsArray[j] / iterations;
}
return resultsArray; return resultsArray;
} }
public static int RollASingle256SidedDie20Times(Random rand) {
int result = 0;
for(int i = 0 ; i < 20; i++)
{
int number = rand.nextInt(256) + 1;
result += number;
}
public static double[] GenerateRandomDistribution(int dieSides, int rolls, int iterations) { return result;
}
public static double[] GenerateRandomDistribution(int dieSides, int rolls, int iterations) {
// DONE AND PASSED TEST
double[] resultsArray = new double[(dieSides*rolls)+1]; double[] resultsArray = new double[(dieSides*rolls)+1];
Random rng = new Random(1234); Random rng = new Random(1234);
for(int i = 0; i<iterations; i++) for(int i = 0; i<iterations; i++)
{ {
int numIGot = 0;
numIGot = RollADieNTimes(rng,dieSides,rolls);
resultsArray[numIGot] = resultsArray[numIGot] + 1;
}
for(int j = 0; j < resultsArray.length; j++)
{
resultsArray[j] = resultsArray[j] / iterations;
} }
return resultsArray; return resultsArray;
} }
public static int RollADieNTimes(Random rand, int dieSides, int rolls) {
int result = 0;
for(int i = 0 ; i < rolls; i++)
{
int number = rand.nextInt(dieSides) + 1;
result += number;
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment