Testing a Java Randomizer and Array Method

My Java Randomizer

Working with Java for the first time. Aiming to eventually create an Android app.  So here is a little bit of a look what I created. Essentially I wanted to use a Java randomizer to make a 20-sided dice roller; eventually it will be implemented in an app.

package javarandom;

 * @author Mikel Morris
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.Arrays;
 import java.util.stream.*;

public class JavaRandom {

 * @param args the command line arguments
 public static void main(String[] args) {
 // TODO code application logic here

int count;
 count = 0;
 int[] a = new int[10]; //creates array
 int nextIndex = 0; //sets the array index
 while (count < 10)
 int s; //Initialize s
 s = ThreadLocalRandom.current().nextInt(); //s is randomized.
 int rNew = Math.abs((s)%20); //gives absolute value limiting 0-19 and makes it rNew.
 rNew = rNew+1; //Changes range from 0-19 to 1-20.
 System.out.println(rNew); //prints to console the dice roll.

 Arrays.fill(a, nextIndex, nextIndex+1, rNew); //fills current Array index with dice roll.
 nextIndex++; // moves the index in the array further.

count++; //count up for while loop
 int sum = IntStream.of(a).sum(); //adds full string together
 double sum2 = sum; //Changes to double getting it ready for division.
 double average = sum2 / 10; //divides by amount of indices.
 System.out.println("The Total of all rolls is " +sum); // Total of all index figures.
 System.out.println("The average of your rolls is " +average); //Average of index.


I printed out the array at each step after adding a random number to see how arrays are built.

the while loop was added  to test that I was getting different numbers. Then I added an array to store them and then I added the values of the array and averaged it. Why? Just to learn a little bit.

If you know anything about Java randomizers then I’d love to hear, please share in the comments. Also try out my code and tell me if you have the same (random) results.

Java Randomizer

I built the same thing in C++ but it was far different.

#include <cstdlib>
#include <ctime>
#include "DiceRoller_D20.h"

using namespace std;


srand(time(0) -rand());
return (rand() %20) +1;

The C++ program seemed better because it felt a little more random but of course I’m just human so who knows if it’s actually more random. I’ll continue to learn both programs and share my progress, even though I’ll need to figure out a dual screen setup instead of a laptop because there is so much switching back and forth between browser and an IDE gets painful.


Random Method Java Documentation
Array Tutorial Java Documentation
Array Method Java Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *