England Forum - UK Forum
 

Go Back   England Forum - UK Forum > Computers and Technology > Developers Forum > Java Forum

 

 


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2008
Peon
 
Join Date: Nov 2008
Posts: 1
Default How do I generate a random number between 1 and 6 in java?

Trying to build a program that rolls random die numbers.
I'm trying to use Math.random to generate random numbers in this for loop. I just need to get the code so it generates a number between 1 and 6 each time - here's the code.

import javax.swing.JOptionPane;

public class Array2 {

public static void main(String[] args) {

int frequency[] = new int[7];
frequency[0] = 1;
frequency[1] = 2;
frequency[2] = 3;
frequency[3] = 4;
frequency[4] = 5;
frequency[5] = 6;

int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;

for(int i=0; i <= 9999; i++)
{
frequency[i] = Math.random()*6;

if(frequency[i] == 1)
count1++;

if(frequency[i] == frequency[1])
count2++;

if(frequency[i] == frequency[2])
count3++;

if(frequency[i] == frequency[3])
count4++;

if(frequency[i] == frequency[4])
count5++;

if(frequency[i] == frequency[5])
count6++;
}

System.out.println(count1);

}
}
Nevermind, I was making a massive cock up. Fixed now.

import javax.swing.JOptionPane;

public class Array2 {

public static void main(String[] args) {

int frequency[] = new int[10000];

int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;

for(int i=0; i <= 9999; i++)
{
frequency[i] = (int)(Math.random()*6) + 1;

if(frequency[i] == 1)
count1++;

if(frequency[i] == 2)
count2++;

if(frequency[i] == 3)
count3++;

if(frequency[i] == 4)
count4++;

if(frequency[i] == 5)
count5++;

if(frequency[i] == 6)
count6++;
}

System.out.println(count1);
System.out.println(count2);
System.out.println(count3);
System.out.println(count4);
System.out.println(count5);
System.out.println(count6);

}
}
I only had JOptionPane imported as a pre-requisite. Incase the next thing I work on needs it!

Cheers fellas.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links

  #2 (permalink)  
Old 11-24-2008
Peon
 
Join Date: Nov 2008
Posts: 2
Default

just look for random number gen class and add it to you code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-24-2008
Peon
 
Join Date: Nov 2008
Posts: 1
Default

JOptionPane is for text input, you dont seem to have any show input dialog methods so i wouldnt think its necessary. Math.random((double))*1000 generates integers so try using that the rest of the details i am a little rusty on.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-24-2008
Peon
 
Join Date: Sep 2008
Posts: 6
Default

import java.util.Hashtable;
import java.util.Random;

public class DieFreqs {

static private Hashtable<Integer, Integer> hashT =
new Hashtable<Integer, Integer>();

public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
hashT.put(new Integer(i), 0);
}

for (int i = 0; i < 10000; i++) {
Integer randomNum = getRand();
int curValue = hashT.get(randomNum);
curValue++;
hashT.put(randomNum, curValue);

}
System.out.println("Frequencies: "+hashT.toString());
}

private static Integer getRand() {
Random r = new Random();

return new Integer( r.nextInt(6) +1);

}

}
//>===================
if you don't want to use modern API, maybe this will give you ideas.

java.util.Random is more convenient than Math.random().
the die is r.nextInt(6) + 1; for 1 to 6 inclusive
Hasthtable is the same as an Array, except we use a key instead of an Index. Hashtable<name, value>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:27 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC7
Sedo - Buy and Sell Domain Names and Websites project info: englanddebate.co.uk Statistics for project englanddebate.co.uk etracker® web controlling instead of log file analysis

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213