Hilbert's Hotel

Diskussionsforum zur Unendlichkeit: Theismus, Atheismus, Primzahlen, Unsterblichkeit, das Universum...
Discussing Infinity: theism and atheism, prime numbers, immortality, cosmology, philosophy...

Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 20:05
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (7th_zorro), 1,300 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 7 1 2 3 4 5 6 7
NVidia Number Test #162283
10/20/07 11:25
10/20/07 11:25
Joined: Jul 2006
Posts: 503
Australia
A
adoado Offline OP

User
adoado  Offline OP

User
A

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


Visit our development blog: http://yellloh.com
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
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

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: TWO] #162285
10/20/07 11:45
10/20/07 11:45
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
your function will never return 3 and 6.

hm... this is tricky. i think i could do it with some if() clauses but i didn't find an elegant solution yet.

Re: NVidia Number Test [Re: ventilator] #162286
10/20/07 12:08
10/20/07 12:08
Joined: Jul 2006
Posts: 503
Australia
A
adoado Offline OP

User
adoado  Offline OP

User
A

Joined: Jul 2006
Posts: 503
Australia
I have been trying to think of a method myself, but yeah - this is hard


Visit our development blog: http://yellloh.com
Re: NVidia Number Test [Re: adoado] #162287
10/20/07 12:12
10/20/07 12:12
Joined: Oct 2002
Posts: 8,939
planet.earth
ello Offline
Senior Expert
ello  Offline
Senior Expert

Joined: Oct 2002
Posts: 8,939
planet.earth
rand7()
{
return rand5()+rand2(); // ;P
}

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
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

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: ello] #162289
10/20/07 12:57
10/20/07 12:57
Joined: Oct 2003
Posts: 4,131
M
Matt_Aufderheide Offline
Expert
Matt_Aufderheide  Offline
Expert
M

Joined: Oct 2003
Posts: 4,131
Code:
int rand7()
{
float r7=0;

for(int i=0; i<7; i++)
{
r7+=rand5();
}

return int((r7/7)*2);
}



This ought to work unless I'm deluding myself.. however its probably not very elegant.. like all my code


Sphere Engine--the premier A6 graphics plugin.
Re: NVidia Number Test [Re: Matt_Aufderheide] #162290
10/20/07 13:06
10/20/07 13:06
Joined: Jul 2006
Posts: 503
Australia
A
adoado Offline OP

User
adoado  Offline OP

User
A

Joined: Jul 2006
Posts: 503
Australia
@ello: If only it was that simple

Lol if NVIDIA started making hardware that could potentially have infinite loops I would be using an ATI card


Visit our development blog: http://yellloh.com
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
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

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
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

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!
Page 1 of 7 1 2 3 4 5 6 7

Moderated by  jcl, Lukas, old_bill, Spirit 

Kompaktes W�rterbuch des UnendlichenCompact Dictionary of the Infinite


Powered by UBB.threads™ PHP Forum Software 7.7.1