Jump to content

Random number generator with a twist?


Badja

Recommended Posts

I'm looking for a random number generator, that chooses a number every hour, and when it does it will be out of 100 (these are player ID's) once the number is chosen, I would like 10 Crystals (special payment) to be given to the number (ID). Is this possible and if so, please help.

 

This is all I have (generator)

 

function createRandomAGTNO() {

  do {

    $agt_no = mt_rand(1,100);

    $valid = true;

    if (preg_match('/(\d)\1\1/', $agt_no))

      $valid = false; // Same digit three times consecutively

    elseif (preg_match('/(\d).*?\1.*?\1.*?\1/', $agt_no))

      $valid = false; // Same digit four times in string

  } while ($valid === false);

  return $agt_no;

}

 

 

Please reply ASAP?

Link to comment
Share on other sites

For choosing a random number each hour, one trick is to seed the RNG according to some value generated by looking at the current time - without minutes or seconds.

mt_srand(date("YmdH"));
echo mt_rand(1, 100);

 

As for the stuff about crystals, you'll have to be more specific on how that all works.

Link to comment
Share on other sites

Basically once the number has been generated:

 

              Crystals are a special item in the game, a reward, their function name is "#Crystal"

 

At each hour, once the number has come up, say 46 (A game ID), that player would get 10 Crystals, because the number the number that had been generated told the next bit of code to give out the 10 crystals.

Link to comment
Share on other sites

// Seed generator
mt_srand((double)microtime()*1000000);

//All your player ids in an array
$players = array();

// Shuffle array
shuffle($players);

// Random array index
$random = mt_rand(0, count($players));

// final winner
$winner = $players[$random];

 

I dont know what you want exactly but maybe this helps....

 

Link to comment
Share on other sites

Random is not really in a true sense very random when comes to computers, you will see what I mean if use it a lot. There are many ways to do random, many ways are very slow, this should do the job.

 

You need to be finding the stored values of player id's, my below example picks one at random from a mysql query. So pulling the one player_id, then call back to that player_id row, insert the crystal_value.

 

You then do something like this with your specific values of course:

 

<?php
$con = mysql_connect('localhost','username','userpassword');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db('databasename', $con);

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$player_id = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " ); 
$query = mysql_query("SELECT* FROM table WHERE player_id = '".$player_id."'");
        $row = mysql_fetch_array($query);
        $player_id = $row['player_id'];
        $crystal_value = $row['crystal_value'];

$crystal_add = "10";//set this to the value you want
$crystal_value = "$crystal_value+$crystal_add";
mysql_query("UPDATE table SET crystal_value='$crystal_value' WHERE player_id='$player_id'");
mysql_close($con);
?>

 

I wrote this code out so you can understand the process, I'm sure you can optimize this more.

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.