java - 2 randoms seem to give near identical result -


thanks in advance taking time @ q. , same problem hope solution...

so basicly have app spins 2 coins @ same time , displays result is.

this method generates first coin...

    public void coinresult1(){      imageview coin1view = (imageview) findviewbyid(r.id.coin1);      random r = new random();      int coin1result = r.nextint(2);       if (coin1result == 0){         coin1view.setbackgroundresource(r.drawable.coinheads);         coinresult1 = 0;      }         else if (coin1result == 1) {             coin1view.setbackgroundresource(r.drawable.cointails);             coinresult1 = 1;         } } 

and second coin

public void coinresult2(){     imageview coin2view = (imageview) findviewbyid(r.id.coin2);      random r = new random();     int coin2result = r.nextint(2);      if (coin2result == 0){         coin2view.setbackgroundresource(r.drawable.coinheads);         coinresult2 = 0;     }         else if (coin2result == 1) {             coin2view.setbackgroundresource(r.drawable.cointails);             coinresult2 = 1;         } } 

this linked onclick() of button check results player selection

public void checkresult(){         coinresult1();         coinresult2();         coinresult = coinresult1 + coinresult2;         if (coinresult == playerselection){             playerwins();             buttonsreset();         }         else {             playerlost();             buttonsreset();         }     } 

now problem have is... results of both coins of 1000 presses this... headsheads 54%

headstails 2%

tailstails 44%

of 1000000 spins same percentages

when each coin result counted seprately

coin1 heads 53% tails 47%

coin2 heads 48% tails 52%

now friend says wrong odds....beacause headstails wasnt high enough percent, hes expecting close 33% each combination @ random

the code seems favour headsheads , tailstails or headstails.....ive tried several times , keep getting low % of headstails

can shead light on whats going on....and causing headstails come out?

hope hear soon

your repeated instantiation of random ruining statistical properties of generator.

you need create 1 instance , pass functions. better still, use field in class.

please refer question concerns on thread safety of random class:is random class thread safe?. seems suggest should synchronize nextint calls.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -