Scriptr
12-15-2011, 03:34 AM
I wrote a java program for a random number generator:
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) throws IOException {
Gen();
Random dc4 = new Random();
int freq[] = new int[5];
for(int roll = 0; roll < 1000; roll++){
++freq[1+dc4.nextInt(4)];
}
FileWriter w = new FileWriter(new File("frequencies.rck"));
w.write("Num\tFrequency\n");
for(int face = 1; face < freq.length; face++){
w.append(face + "\t" + freq[face] + "\n");
}
w.close();
int yn = JOptionPane.showConfirmDialog(null, "Done. Show roll stats?", "Done", JOptionPane.YES_NO_OPTION);
if (yn == JOptionPane.YES_OPTION){
Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
String r0 = rraw.next();
String r1 = rraw.next();
String r2 = rraw.next();
String r3 = rraw.next();
String r4 = rraw.next();
String r5 = rraw.next();
String r6 = rraw.next();
String r7 = rraw.next();
String r8 = rraw.next();
String r9 = rraw.next();
JOptionPane.showMessageDialog(null, r0 + ": " + r1 + "\n" + r2 + ": " + r3 + "\n" + r4 + ": " + r5 + "\n" + r6 + ": " + r7 + "\n" + r8 + ": " + r9, "Frequencies", JOptionPane.PLAIN_MESSAGE);
}
}
public static void Gen(){
int count = 0;
Random dc4 = new Random();
while (count == 0){
String xt = JOptionPane.showInputDialog(null, "How many numbers?");
int x = Integer.parseInt(xt);
for (int ct = 0; ct < x; ct++){
int d4raw = dc4.nextInt(4);
int d4 = d4raw + 1;
String d4o = Integer.toString(d4);
JOptionPane.showMessageDialog(null, d4o, "Random Number", JOptionPane.PLAIN_MESSAGE);
}
int ns = JOptionPane.showConfirmDialog(null, "All done. New set?", "Done!", JOptionPane.YES_NO_OPTION);
if (ns == JOptionPane.YES_OPTION){
count = 0;
}else{
count = 1;
}
}
JOptionPane.showMessageDialog(null, " Recording Data;\n Hit \"Okay\" to continue", "Done!", JOptionPane.PLAIN_MESSAGE);
}
}
and then I wrote a .bat file to run it:
@echo off
copy Main.class C:\Users\Public\Downloads\src
cd C:\Users\Public\Downloads\src
set path=%path%;C:\Program Files (x86)\Java\jre7\bin
java Main
In theory, both should have worked on any computer that has JRE7. None work on a computer that has not been configured to run Java by me.
How can I make a universally operable .bat (or if needed .exe; I am learning C++) file to make it go?
Don't complain about the script. I know it is inefficient (but there is a reason for the Scanner import, I just haven't coded it in yet), and would appreciate it if you could help me make the proof of randomness thing work more efficiently, but it works, so if all you are going to do is complain, don't. No offence to the community, but that's all I have received from people that examine the script.
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) throws IOException {
Gen();
Random dc4 = new Random();
int freq[] = new int[5];
for(int roll = 0; roll < 1000; roll++){
++freq[1+dc4.nextInt(4)];
}
FileWriter w = new FileWriter(new File("frequencies.rck"));
w.write("Num\tFrequency\n");
for(int face = 1; face < freq.length; face++){
w.append(face + "\t" + freq[face] + "\n");
}
w.close();
int yn = JOptionPane.showConfirmDialog(null, "Done. Show roll stats?", "Done", JOptionPane.YES_NO_OPTION);
if (yn == JOptionPane.YES_OPTION){
Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
String r0 = rraw.next();
String r1 = rraw.next();
String r2 = rraw.next();
String r3 = rraw.next();
String r4 = rraw.next();
String r5 = rraw.next();
String r6 = rraw.next();
String r7 = rraw.next();
String r8 = rraw.next();
String r9 = rraw.next();
JOptionPane.showMessageDialog(null, r0 + ": " + r1 + "\n" + r2 + ": " + r3 + "\n" + r4 + ": " + r5 + "\n" + r6 + ": " + r7 + "\n" + r8 + ": " + r9, "Frequencies", JOptionPane.PLAIN_MESSAGE);
}
}
public static void Gen(){
int count = 0;
Random dc4 = new Random();
while (count == 0){
String xt = JOptionPane.showInputDialog(null, "How many numbers?");
int x = Integer.parseInt(xt);
for (int ct = 0; ct < x; ct++){
int d4raw = dc4.nextInt(4);
int d4 = d4raw + 1;
String d4o = Integer.toString(d4);
JOptionPane.showMessageDialog(null, d4o, "Random Number", JOptionPane.PLAIN_MESSAGE);
}
int ns = JOptionPane.showConfirmDialog(null, "All done. New set?", "Done!", JOptionPane.YES_NO_OPTION);
if (ns == JOptionPane.YES_OPTION){
count = 0;
}else{
count = 1;
}
}
JOptionPane.showMessageDialog(null, " Recording Data;\n Hit \"Okay\" to continue", "Done!", JOptionPane.PLAIN_MESSAGE);
}
}
and then I wrote a .bat file to run it:
@echo off
copy Main.class C:\Users\Public\Downloads\src
cd C:\Users\Public\Downloads\src
set path=%path%;C:\Program Files (x86)\Java\jre7\bin
java Main
In theory, both should have worked on any computer that has JRE7. None work on a computer that has not been configured to run Java by me.
How can I make a universally operable .bat (or if needed .exe; I am learning C++) file to make it go?
Don't complain about the script. I know it is inefficient (but there is a reason for the Scanner import, I just haven't coded it in yet), and would appreciate it if you could help me make the proof of randomness thing work more efficiently, but it works, so if all you are going to do is complain, don't. No offence to the community, but that's all I have received from people that examine the script.