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

I have 2-10 records with a photo.

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

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: [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/