Jump to content

Could someone verify this code?


joecooper

Recommended Posts

Hi,

 

I have created some code that picks a winning lottery ticket number based on the New York "Take 5" lottery. It uses the winning number (as an offically generated random number that i cannot cheat with) as a seed for the rand() function.

 

This way it will always give the same result for that particular seed making it 100% transparent and that i cannot manipulate the results (without changing the code which will be published).

 

The code is:

<?php


if (isset($_POST['submit'])){

$seed = $_POST['take5numbers'];
$ticketssold = $_POST['tickets'];
srand ($seed*13579);
$random_number = rand(1,$ticketssold);
echo "Winning ticket is $random_number";

}else{
?>

<form name="form1" method="post" action="result.php">
"Take 5" numbers from low to high (including leading 0's) <input type="text" name="take5numbers" id="end" size="20" /><br>
Total tickets sold for round <input type="text" name="tickets" id="end" size="20" /><br>
<input type="submit" name="submit" id="submit" value="Get Winning Ticket Number!">
</form>


<?
}
?> 

 

Can someone please tell me if the code is generating fair and unbias results?

Link to comment
Share on other sites

This algorithm is very predictable assuming you don't cut off entering before the numbers are announced.

 

Beyond that, it's a decent way to make a random number table that can be replicated.

 

I understand what you were asking for in the last thread now. You're piggy-backing a well known random system to generate a verifiable lottery yourself.

 

Keep in mind this though, for portability

Niels Keurentjes 25-Feb-2011 01:22

Keep in mind that the Suhosin patch which is installed by default on many PHP-installs such as Debian and DirectAdmin completely disables the srand and mt_srand functions for encryption security reasons. To generate reproducible random numbers from a fixed seed on a Suhosin-hardened server you will need to include your own pseudorandom generator code.

 

As a workaround

To generate a random number which is different every day, I used the number of days after unix epoch as a seed:

 

<?php 

  srand(floor(time() / (60*60*24)));

  echo rand() % 100;

?>

 

My provider upgraded the php server recently, and calling srand(seed) does not seem to set the seed anymore. To let srand set the seed, add the following line to your .htaccess file

 

php_value suhosin.srand.ignore 0

 

Kudos to doc_z (http://www.webmasterworld.com/php/3777515.htm)

 

Harmen

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.