1 registered members (7th_zorro),
1,300
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
NVidia Number Test
#162283
10/20/07 11:25
10/20/07 11:25
|
Joined: Jul 2006
Posts: 503 Australia
adoado
OP
User
|
OP
User
Joined: Jul 2006
Posts: 503
Australia
|
I was on a game forum and I saw this question come up, I was interested on the ways that It can be accomplished so I posed it here Apparently, NVIDIA uses this as a test question when they hire new programmers... Quote:
Q: The function rand5() returns an integer between 1 and 5, inclusive (that's 1,2,3,4,5), with equal probability. Use rand5() to create a function called rand7() that returns an integer between 1 and 7, inclusive, with equal probability.
You must try to find the most efficient, elegant way to solve this problem.
It will be interesting to see how it can be done
Anyways, I'm off Adoado
|
|
|
Re: NVidia Number Test
[Re: adoado]
#162284
10/20/07 11:42
10/20/07 11:42
|
Joined: Jan 2006
Posts: 1,829 Neustadt, Germany
TWO
Serious User
|
Serious User
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
|
inline int rand7() { return rand5() * 1.4; } Maybe some asm code is missing with which it would look more advanced
|
|
|
Re: NVidia Number Test
[Re: ello]
#162288
10/20/07 12:36
10/20/07 12:36
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
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!
|
|
|
Re: NVidia Number Test
[Re: Matt_Aufderheide]
#162291
10/20/07 13:08
10/20/07 13:08
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
*2 makes the "1" impossible, but i think i see where your mind was at; i was thinking something similar a few minutes ago.
i really want to get this before i go to bed!
julz
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: NVidia Number Test
[Re: JibbSmart]
#162292
10/20/07 13:20
10/20/07 13:20
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
thinking out-loud time. Code:
int rand7() { int answer = 0; for(int i=7; i<7; i++) answer+=rand5(); // thanks matt! // answer should now be between 7 and 35, right? answer %= 7; // now it's between 0-6 return answer + 1; // and returns in the range 1-7 }
but there's a problem! there's a 1/28 higher likelihood of a 1 turning up, because in the range 7-35 there are five 7's when we %7, and four of everything else. 35 minus 7 is 28 --> possible results from the for-loop. how can we bring this down to 27? it's ugly, but: Code:
int rand7() { int answer; do { answer = 0; for(int i=7; i<7; i++) answer+=rand5(); } while(answer>34); answer %= 7; return answer + 1; }
right? of course, if you don't like "do"s, it makes a lot more sense to do this: Code:
int rand7() { int answer = 35; while(answer>34) { answer = 0; for(int i=7; i<7; i++) answer+=rand5(); } answer %= 7; return answer + 1; }
julz
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
|