joecooper Posted August 23, 2011 Share Posted August 23, 2011 Hi, I need to write some code for this but unsure where to start. Basically i have a database of say 10 rows. I need to be able to input a number and some sort of fair algorithm will choose one of the rows. The number will be from 6 to 10 numbers long, and will need to pick the same result each time based on that number. Its for a lotto game Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/ Share on other sites More sharing options...
voip03 Posted August 23, 2011 Share Posted August 23, 2011 You can use rand() select table. http://php.net/manual/en/function.rand.php Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1260908 Share on other sites More sharing options...
joecooper Posted August 23, 2011 Author Share Posted August 23, 2011 But for a lotto game to be transparent so people can see Im not manipulating results, then it needs to be based on a real lottery draw. so they can test the code to make sure its picking a fair result. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1260910 Share on other sites More sharing options...
joecooper Posted August 23, 2011 Author Share Posted August 23, 2011 bumpty bump? Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1260975 Share on other sites More sharing options...
xyph Posted August 23, 2011 Share Posted August 23, 2011 I'm not exactly sure what you're asking? Trying to make an algorithm so a 6-10 digit number will coincide with a number between 1-10? More details are needed. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1260986 Share on other sites More sharing options...
joecooper Posted August 23, 2011 Author Share Posted August 23, 2011 yeah thats pretty much it. Each person buys a lottery ticket and a new row is created for them in a mysql database. now to make the lottery fair, it needs to be based on a real lottery's random numbers. My lottery will pick a random row, but it needs to be based on the lottery numbers, so that people can check that the row is correct and that i did not cheat. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261010 Share on other sites More sharing options...
xyph Posted August 23, 2011 Share Posted August 23, 2011 Does there HAVE to be a winner? You seem to just want to make it seem like it's a fair lottery, rather than actually compute a fair lottery. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261123 Share on other sites More sharing options...
jcbones Posted August 24, 2011 Share Posted August 24, 2011 Run it like a real lotto, where the jackpot stacks up until someone wins. Then you could generate a random number to put into the row, or let them pick their own number. If two numbers come up, they split the prize, or make the row unique, kicking it back if it fails. If you MUST have a winner, draw until you find one. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261145 Share on other sites More sharing options...
joecooper Posted August 24, 2011 Author Share Posted August 24, 2011 But that allows me to manipulate the winning number and choose a winner. I have to have a set code that generates the same 1-10 number each time. like a seed i guess Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261238 Share on other sites More sharing options...
joecooper Posted August 24, 2011 Author Share Posted August 24, 2011 SRand() is what i was looking for . That way people can check im not cheating and that a row was really chosen at random Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261239 Share on other sites More sharing options...
joecooper Posted August 24, 2011 Author Share Posted August 24, 2011 Actually... Im not too sure if this is working correctly, could anyone check this for me. <?php $seed = "040935702352" ; srand ($seed*12345); $random_number = rand(1,100); echo "$random_number"; ?> $seed being the lottery number. It returns the same number for each seed which i think is fair. but for some reason sometimes if i change the number slightly (any number after an 8 or 9) then the result stays the same. i added in the *12345 to make it more random (i guess) Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261242 Share on other sites More sharing options...
xyph Posted August 24, 2011 Share Posted August 24, 2011 Are you sure you understand what seed is doing? Random doesn't seem like something you're really looking for. You want a set output for a set input, and that's not random. Want a winner every time? Screw the whole number system, just throw userid's in a table, and pick one at random. This can be proven, in code, that a random row has been chosen. Want a number system with a guaranteed winner? You're going to have to assign numbers to players, not allow them to choose. Want a system where players pick their numbers, and winning numbers are chosen randomly, and a winner is decided based on that? jcbones has the solution. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261467 Share on other sites More sharing options...
yourichi Posted August 24, 2011 Share Posted August 24, 2011 Using random you wont really be able to provide the same result everytime. and if I understand correctly, your asking to randomly pick someone , but you have to be able to repeat picking the same person as proof? doesnt that kind of defeat the point of it being random? O_o You have to prioritize which function is actually more important to the game itself. To ensure you'll always have a winner you need the "random" to have the exact range as the number of rows ('players') You want to use a seperate number entered ie winning lottery numbers aspect, well they purely base their sense from having multiple random numbers drawn from a 'hat' but you want to enter this number? therefore taking away the 'lottery' aspect imo. Why not broaden the controls of your 'game' and make it more like an actual game, get each contestant to pick say 5 numbers from 20 for example. (if your looking at a low number of people entering for a higher odds at getting a winner you specify to your players pick 1 from each list in say a drop down list on a form with 5 lists each with the below range) $randomnumber1 = rand(1,4) $randomnumber2 = rand(5, $randomnumber3 = rand(9,12) $randomnumber4 = rand(13,16) $randomnumber5 = rand(17,20) winner = someone who gets 3/5 matchs for example. depending on what exactly your trying to achieve. Somehow to me it reads like your trying to pretend its fair which if was the case it might look like.. players table would look like | idticketnumber | winner | loser | randomnumber | | 1 | 0 | 0 | | | 2 | 0 | 0 | | etc etc $myrandomkey = $_GET['number']; resultQuery = SELECT `idticketnumber` `where randomnumber` == $myrandomnumber if ( resultquery = false ){ $winner = rand(1,10) $query = "UPDATE set lotterytable `winner` = '1' `randomnumber` = '$myrandomkey' where id=$winner"; } else echo idticketnumber you get the idea.. Quote Link to comment https://forums.phpfreaks.com/topic/245495-pick-an-item-from-database-based-on-a-number/#findComment-1261503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.