0 registered members (),
1,321
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: NVidia Number Test
[Re: ventilator]
#162294
10/20/07 22:40
10/20/07 22:40
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
when i woke up this morning i realised that mine's still wrong -- it still suffers from dice syndrome.
ventilator, as elegant as your solution is, it suffers from the same problem. there are, of course, 7 different possible results, but there are 5*5*5*5*5 = 3125 different ways to get those results. 7 is not a factor of 3125. of course, that's only a little sign that something does NOT have 7 results with equal probability. if it were divisible by 7, it wouldn't necessarily have avoided the problem i'm about to explain as follows:
a "1" will be returned whenever rand5()+...rand5()-2 < 6 (if i'm right in assuming that type conversion to integer doesn't round, but instead truncates the number). this will happen when rand5()+...rand5() < 8. now, it would take way too long to count out the number of combinations. but, it does require a result between 5-7, and that's the bottom range of this whole process. 5 can only be obtained the following way: 1+1+1+1+1 6 is much more likely to be obtained: 1+1+1+1+2 1+1+1+2+1 1+1+2+1+1 1+2+1+1+1 2+1+1+1+1 and as we get closer to the middle ((25-5+1)/2+5=15.5, which should be exactly halfway between 5 and 25) we get more possible combinations, and so the number with the highest probability of being obtained is (15-2)/3 = ~4, (16-2)/3 = ~4, because its rand5()+...+rand5() results need to be between 14-16, which have the highest number of combinations attributed to them.
which makes sense, of course, because 4 is exactly halfway between 1 and 7.
this basically explains the "dice syndrome" i mentioned earlier, which we're all yet to overcome.
julz
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: NVidia Number Test
[Re: JibbSmart]
#162295
10/20/07 23:02
10/20/07 23:02
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
i've got it: rand5() + rand5()*5 has a result between 6 and 30, with only one possible combination for each answer <-- that's really nice, but what can we do with the 25 different results? we can take just 21 of them. if we get higher than the 21st result, just do it again. Code:
int rand7() { int answer = 27; while(answer>26) answer = rand5() + rand5()*5; return (answer-5)%7+1; }
i think that's it. julz edited to return the answer
Last edited by JulzMighty; 10/21/07 00:50.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: NVidia Number Test
[Re: JibbSmart]
#162296
10/20/07 23:33
10/20/07 23:33
|
Joined: May 2002
Posts: 7,441
ventilator
Senior Expert
|
Senior Expert
Joined: May 2002
Posts: 7,441
|
you are right! mine doesn't give equal probabilites. here is a simple python test script: Code:
import random
def rand5(): return int((random.random() * 5) + 1)
##def rand7(): ## return (rand5()+rand5()+rand5()+rand5()+rand5() - 2) / 3
def rand7(): answer = 27 while answer > 26: answer = rand5() + rand5() * 5 return (answer - 5) % 7 + 1
count = {} for i in range(1000000): r = rand7() if count.has_key(r): count[r] += 1 else: count[r] = 1
print count
your version works! but is a version without while or if possible? <edit>hm... i am not sure but this one seems to work: Code:
def rand7(): return ((rand5() + 5*rand5() + 25*rand5() + 125*rand5() + 625*rand5() + 3125*rand5() + 15625*rand5() - 19531) % 7) + 1
i only found this with some trial and error though! </edit>
|
|
|
Re: NVidia Number Test
[Re: ventilator]
#162297
10/21/07 00:58
10/21/07 00:58
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
good question -- branches and loops are ugly! if we can find a power of 5 that is divisible by 7, then my version could be changed a little to do without the while-loop. the trouble is that there aren't any i'm glad it works though i have loads of study to do for tomorrow and i couldn't justify taking the time to test it. would you mind explaining how your test works? i don't know anything about python, so that last bit is gibberish to me. is it counting how many of each number is returned? julz
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: NVidia Number Test
[Re: JibbSmart]
#162298
10/21/07 07:08
10/21/07 07:08
|
Joined: Oct 2003
Posts: 4,131
Matt_Aufderheide
Expert
|
Expert
Joined: Oct 2003
Posts: 4,131
|
Quote:
*2 makes the "1" impossible
oops ..it cant work anyway becasue of the progression of the products.. oh well.. i am not good at this kind of thing.
|
|
|
Re: NVidia Number Test
[Re: Michael_Schwarz]
#162300
10/22/07 16:19
10/22/07 16:19
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
Expert
|
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
i tend to think it's not possible at all since both 5 and 7 are prime numbers and rand5 can't return 0. edit: ok, i found a way... at least all numbers are equally probable here. not very elegant, though. Code:
short int rand7() { short int temp = 0;
for (int i = 0; i < 8; i ++) { temp += rand5()-1; }
if (temp == 32) { return rand7(); }
return temp/4+1; }
Last edited by Joey; 10/22/07 16:27.
|
|
|
Re: NVidia Number Test
[Re: ventilator]
#162302
10/22/07 17:53
10/22/07 17:53
|
Joined: Jul 2000
Posts: 8,973 Bay Area
Doug
Senior Expert
|
Senior Expert
Joined: Jul 2000
Posts: 8,973
Bay Area
|
I'm must be missing something because this sounds too easy: Code:
ans = (((rand5()-1) / 4f)*6f) + 1;
You could reduce this down, but I left it long so you can follow the logic. Code:
(rand5()-1) -> random number 0-4 / 4 -> random number 0-1 * 6 -> random number 0-6 + 1 -> random number 1-7
|
|
|
|