//you can use only one rand obj to use Random()
Random rand = new Random();
//Initializing coins as 0
//Values of coins will change once we get random
int coin1 = 0;
int coin2 = 0;
int coin3 = 0;
//initializing heads and count as 0
int heads = 0;
int count = 0;
while(heads < 3){
//Assume '1' comes out for head and '0' for tail
coin1 = rand.nextInt(2);
coin2 = rand.nextInt(2);
coin3 = rand.nextInt(2);
//if all three coins are '1' which is head;
//sum should be 1+1+1 = 3
//while loop terminates because 3<3 is false
heads = coin1+coin2+coin3;
count++; //count = count +1;
}
//printing the value of count and coins
System.out.println(count + "\t" +coin1 + "\t" + coin2 + "\t" +coin3);
Самый простой способ - использовать 0,0 / 0,0
. «NaN» .to_f не работает, и в этой теме обсуждается, почему.
0.0 / 0.0
работает для меня на ruby 1.8.6.
У нити, связанной с Pesto, есть эта функция, которая должна работать на платформах, где реализованы числа с плавающей запятой согласно IEEE 754:
def aNaN
s, e, m = rand(2), 2047, rand(2**52-1)+1
[sprintf("%1b%011b%052b", s,e,m)].pack("B*").unpack("G").first
end