random but with odds
4 posts by 3 authors in: Forums > CMS Builder
Last Post: March 9, 2012 (RSS)
By rez - March 7, 2012 - edited: March 7, 2012
When arriving to the page, I want 1 photo / record to appear randomly. BUT, I would like to be able to tweak the odds for each record. Think of a contest where record 1 is "sorry, try again". Record two is the "biggest prize" and the rest are smaller offers. I would of course like record 1 to appear the most. I know how to set up all the records and I realize that a secure contest that cant be cheated or even refreshed is a lot more complicated than this. This is a simplified example to get code I dont understand.
I have seen how to randomly make a 1 record show on a page but how can I control the odds? Maybe I want the try again graphic to show up say, 50 times out of 100.
help with this would be appreciated. :)
Re: [rez] random but with odds
By Dave - March 7, 2012
You're getting into some more advanced mysql here, but check out this post: http://stackoverflow.com/questions/1756465/mysql-weight-based-selection
Hope that helps!
interactivetools.com
Re: [Dave] random but with odds
By rez - March 9, 2012 - edited: March 9, 2012
Once that was working, i was going to make 1-3 different winner graphics and play with the the number 10 to make a winner appear less.
I know my "where" must be wrong because the myNum is coming up as undefined.... or i am not using random right. im trying to select a random number from 1-10.
I guess to get record 1-3 as winners, i just go through if statements, then use an else to show anything else as try again?
Again, I know page refreshes and that sort of thing make this kind of ridiculous but once i understand it, maybe i can expand this simple idea.
so right now, record 1 is a winning graphic. record 2 is a sorry graphic. not working:
<?php $rndNum = rand(1,10); ?>
<?php if ($rndNum==1): ?>
<?php $myNum=1 ?>
<?php endif ?>
<?php if (!$rndNum==1): ?>
<?php $myNum=2 ?>
<?php endif ?>
<?php
// load records
list($contestRecords, $contestMetaData) = getRecords(array(
'tableName' => 'contest',
'where' => "num = '$myNum'"
));
?>
Re: [rez] random but with odds
By Jason - March 9, 2012
Give this a try:
<?php
if (rand(1,10) == 1) {
$myNum = 1;
}
else {
$myNum = 2;
}
// load records
list($contestRecords, $contestMetaData) = getRecords(array(
'tableName' => 'contest',
'where' => "num = '$myNum'"
));
?>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/