程式的功力差別!!!!

今天在寫apollo的ad 部份
有一個部份是在實做百面骰的部份
用百面骰的概念來實做百分比的呈現
例如我有5個值
a 的 比例是20,
b 的 比例是30,
c 的 比例是50,
d 的 比例是80,
f 的 比例是160
然後依比例來讓他們呈現應有的次數
最直接想的是
直接塞個20+30+50+80+160 的array
不過覺得這樣做實在是挺白痴的
覺得應該有更好的方法
經過”貓哥”的小小提示”區間”
我想到的是
$rand = rand(1,$ad_total);
用for 來做
if (0 <= $rand<= a){
return a;
} elseif ( a < $rand <=b ){
return b;
}
一直下去。
不過貓哥寫的是這樣
$rand = rand(1,$ad_total);
for ($i=0; $i $rand = $rand – $rand_row[$i]['percentage'];
if ($rand < 0) {
break;
}
}
一看就覺得我自已像笨蛋一樣…………………..
有很深的体會,程式寫的好的和不好的差別,真的是一眼就看出來了。 ::S:

About johnpupu

..........
This entry was posted in program and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word