random but with odds

4 posts by 3 authors in: Forums > CMS Builder
Last Post: March 9, 2012   (RSS)

Re: [rez] random but with odds

By Dave - March 7, 2012

Hi Rez,

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!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] random but with odds

By rez - March 9, 2012 - edited: March 9, 2012

trying to do something like below to get some odds going. The idea is that on page load, i get a random number from 1 to 10. if the number is 1, show the winner graphic. Anything else, show the losing graphics.

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

Hi,

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/