Code:
int rand7()
{
int answer = 8;
while(answer>7)
answer = rand5()+rand5()-1; // number between 1 and 9
return answer;
}



best i could do in a minute. sort of elegant, but could potentially be an infinite loop (theoretically, but i think a computer's "random" table would cause a number 7 or lower to pop up fairly quickly).

julz

EDIT: oooh sorry! just realised while eating that each number does not have equal probability of turning up -- it's similar to the probability of '7' turning up when throwing two dice being higher than any other number.

Last edited by JulzMighty; 10/20/07 13:02.

Formerly known as JulzMighty.
I made KarBOOM!