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
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,466 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 3 of 7 1 2 3 4 5 6 7
Re: NVidia Number Test [Re: Doug] #162303
10/22/07 17:57
10/22/07 17:57
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
you still will only get 5 different values. it's like TWO's try.

Re: NVidia Number Test [Re: ventilator] #162304
10/22/07 18:25
10/22/07 18:25
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
Quote:

no, the latest two both don't give equal probabilities and i think the range of returned numbers is wrong too.




why that?

Code:
unsigned short int rand7() {
static unsigned short int temp = 0;

return (temp += rand5(), temp %= 7);
}



Last edited by Joey; 10/22/07 18:27.
Re: NVidia Number Test [Re: Joey] #162305
10/22/07 18:33
10/22/07 18:33
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
julzmighty has explained the problem with the more likely combinations.

...
i had a new idea:
Code:
s = 1
def rand7():
global s
s = 1 - s
result = 0
if rand5() > (2+s): result += 1<<0
if rand5() > (2+s): result += 1<<1
if rand5() > (2+s): result += 1<<2
return result

but it uses a global variable and 0 and 7 are about 15% more probable than the rest of the numbers.

...
i wonder how much time the candidates have for this question at nvidia and how many come up with a correct solution. or is this some very well known problem you learn about if you study computer science at an university?

Re: NVidia Number Test [Re: ventilator] #162306
10/22/07 18:41
10/22/07 18:41
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
Code:
temp = 0
def rand7():
global temp
temp += rand5()
return temp % 7

at a first glance joey's last one looks like the most elegant solution so far.

Re: NVidia Number Test [Re: ventilator] #162307
10/22/07 18:46
10/22/07 18:46
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
but some numbers seem predictable, such if you get a 1 p.ex. you can be sure not to retreive a 7 or one in the next round. but if you're caching any value inside a random function you can only reduce such an effect, never extinguish it, by making the function more complex.

Re: NVidia Number Test [Re: Joey] #162308
10/22/07 19:07
10/22/07 19:07
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
such predictability is bad... darn, i already thought it's THE solution after i let it run through my python test.

Re: NVidia Number Test [Re: ventilator] #162309
10/22/07 20:04
10/22/07 20:04
Joined: Jul 2000
Posts: 8,973
Bay Area
Doug Offline
Senior Expert
Doug  Offline
Senior Expert

Joined: Jul 2000
Posts: 8,973
Bay Area
Quote:

you still will only get 5 different values. it's like TWO's try.




I missed the "integer" part.

My updated answer (just off the top of my head):
Code:

ans = (rand5()+rand5()+rand5()+rand5()+rand5()+rand5()+rand5()) / 7;



6 adds, 1 div, and 7 function calls.

Not a great answer, just a first draft.




Conitec's Free Resources:
User Magazine || Docs and Tutorials || WIKI
Re: NVidia Number Test [Re: ventilator] #162310
10/22/07 20:13
10/22/07 20:13
Joined: Jun 2005
Posts: 4,875
broozar Offline
Expert
broozar  Offline
Expert

Joined: Jun 2005
Posts: 4,875
ok, at first i solved the probabilities:



each arrow from ran5() is 1/5, each from rand2() is 0.5

rand5() is known
rand2() is (rand()%2)+6

so the function is (not elegant, but correct, i assume):

rand7(){
switch ((rand()%7)){ //first level: determin rand5() or rand2()
case 0: case 1: rand2(); break; // 2/7 prob
case 2: case3: case 4: case 5: case 6: rand5(); break; // 5/7 prob
}}

Re: NVidia Number Test [Re: broozar] #162311
10/22/07 20:19
10/22/07 20:19
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
doug, this way the probabilites aren't equal. see julzmighty's explanation. and you made a small mistake. after -7 the range is 0..28.

Code:
(rand5()+rand5()+rand5()+rand5()+rand5() - 2) / 3

gives this distribution after 1 million runs:
{1: 6864, 2: 72170, 3: 243728, 4: 355145, 5: 243124, 6: 72255, 7: 6714}



did anyone have a thought about the one i posted earlier?
Code:
(rand5() + 5*rand5() + 25*rand5() + 125*rand5() + 625*rand5() + 3125*rand5() + 15625*rand5() - 19531) % 7) + 1

is there some hidden flaw? the result seems to be correct:
{1: 143263, 2: 142785, 3: 142579, 4: 142533, 5: 143609, 6: 142520, 7: 142711}

Re: NVidia Number Test [Re: ventilator] #162312
10/22/07 20:30
10/22/07 20:30
Joined: Jun 2005
Posts: 4,875
broozar Offline
Expert
broozar  Offline
Expert

Joined: Jun 2005
Posts: 4,875
Quote:

rand5() + 5*rand5() + 25*rand5() + 125*rand5() + 625*rand5() + 3125*rand5() + 15625*rand5() - 19531) % 7) + 1



is nothing but
Code:
(very_large_nuber)%7+1

which is
Code:
rand()%7+1


of course that's correct, but why would you need 7 function calls, 6 multiplicatios... for such a simple task? you do not really use rand5(), it's only a factor to make your factor in the brackets bigger, you could have used rand6(), rand9() etc. without any change in the result.

Page 3 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