# Random numbers, rolling dices, strange results

Page 1 of 1

## 4 Replies - 672 Views - Last Post: 17 March 2009 - 06:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=93139&amp;s=ef17e14bd1545073a7f73edd8bceb10b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rumo

Reputation: 0
• Posts: 9
• Joined: 14-December 08

# Random numbers, rolling dices, strange results

Posted 16 March 2009 - 04:05 AM

Hi guys!

I've got some strange results, which i can't explain.
I roll a die for say 100.000.000 times, add all results and divide by the number of the rolls.

This is how i roll a die:

```
public static int rollDice(){

int result;

Random r = new Random();
int randint = r.nextInt(6);

result=randint+1;

return result;
}

```

This is how i do the calculations:

```
public static void main(String[] args) {

long sum,result, n;
double mean;

n=100000000;
sum =0;
result =0;

System.out.println(n+" rolls");

for(int i=0;i<n;i++){
result = rollDice();
sum=sum+result;
}

mean=sum;
mean=mean/n;

System.out.println("relative frequency: "+mean);
}

```

sample output:

100000000 rolls
relative frequency: 3.49980715

Of course the relative frequency (->mean) should approximate the expected value of a single roll, which is 3.5. To be more precise, after this number of rolls the result should lie within 3.5 +/- 0.001 with a probability of 99,9%.

This seems to work.

But now comes the riddle.

In a separate sum (->vsum) i add only those dices, which don't show a six and still divide by the total number of rolls.

The relative frequency (->vmean) of this modified counting should approximate 2.5, again 2.5 +/- 0.001 with a probability of 99,9%.

I take the original main method, and add some more lines:
```public static void main(String[] args) {

long sum,result, n,vsum;

n=100000000;
System.out.println(n+" rolls");
double mean, vmean;

sum =0;
vsum =0;
result =0;

for(int i=0;i<n;i++){

result = rollDice();
sum=sum+result;
if(result != 6){
vsum = vsum + result;
}
}

mean=sum;
mean=mean/n;

vmean=vsum;
vmean=vmean/n;

System.out.println("relative frequency: "+mean);
System.out.println("relative frequency, modified counting: " +vmean);
}

```

sample output:

100000000 rolls
relative frequency: 3.50447067
relative frequency, modified counting: 2.51237313

Not only the modified value is way out of bounds, but also the original one is too high!
I've checked several times, the results for the variable mean are constantly too high, but i didn't change anything in the code which should affect this variable.

Right now i'm using widows/eclipse, but i think i had similar results with linux/console (i'll check on this).

Something for the X-files?

Cheers, Rumo

Is This A Good Question/Topic? 0

## Replies To: Random numbers, rolling dices, strange results

### #2 Toxicterror

• blub

Reputation: 15
• Posts: 687
• Joined: 10-March 09

## Re: Random numbers, rolling dices, strange results

Posted 16 March 2009 - 05:07 AM

in the second code sequence you're only adding only throws that are in the range of 1 and 5 (a dice with 5 instead of 6 sides). So everytime you get a 6, you're not adding it to the result.
That's the reason why your result is so inaccurate.

This post has been edited by Toxicterror: 16 March 2009 - 05:10 AM

### #3 Rumo

Reputation: 0
• Posts: 9
• Joined: 14-December 08

## Re: Random numbers, rolling dices, strange results

Posted 16 March 2009 - 05:22 AM

Toxicterror, on 16 Mar, 2009 - 04:07 AM, said:

in the second code sequence you're only adding only throws that are in the range of 1 and 5 (a dice with 5 instead of 6 sides). So everytime you get a 6, you're not adding it to the result.
That's the reason why your result is so inaccurate.

The second sum is meant to be that way, the value vmean should group around 2.5, because the sixes are not counted. It does, but not well enough. Why not?

And the other question is, why the value of mean also seems to have risen, although nothing has changed.

Cheers

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Random numbers, rolling dices, strange results

Posted 16 March 2009 - 05:44 PM

Actually not a good idea at all to create 1,000,000 Random object
Create only one Random object and use it for your 1,000,000 calls that will garanty randomly distributed numbers

### #5 Rumo

Reputation: 0
• Posts: 9
• Joined: 14-December 08

## Re: Random numbers, rolling dices, strange results

Posted 17 March 2009 - 06:17 AM

Thx dude, that did the trick!

I thought creating a new object each time would only consume resources and not affect the outcome. so i didn't bother.
(i know, bad style, shame on me )