Badja Posted November 29, 2010 Share Posted November 29, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/ Share on other sites More sharing options...
requinix Posted November 29, 2010 Share Posted November 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141002 Share on other sites More sharing options...
Badja Posted November 29, 2010 Author Share Posted November 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141006 Share on other sites More sharing options...
requinix Posted November 29, 2010 Share Posted November 29, 2010 Okay, not the kind of "more specific" I was thinking of. I'm guessing this isn't your code? Ask whoever made it how you should do the bit with adding crystals. Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141017 Share on other sites More sharing options...
Badja Posted November 29, 2010 Author Share Posted November 29, 2010 I'm doing this for someone else, yes, but he knows less code than me, I did the generator code myself. Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141023 Share on other sites More sharing options...
MrXHellboy Posted November 29, 2010 Share Posted November 29, 2010 // 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.... Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141031 Share on other sites More sharing options...
Badja Posted November 29, 2010 Author Share Posted November 29, 2010 Thanks, I'll see what he says Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141032 Share on other sites More sharing options...
QuickOldCar Posted November 29, 2010 Share Posted November 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141045 Share on other sites More sharing options...
QuickOldCar Posted November 30, 2010 Share Posted November 30, 2010 I decided to test my above code within my website. Using the same random code above, except instead of inserting a value..I instead fetch the results from the id, and display it. So here's a random websites display. http://dynaindex.com/random-url Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141176 Share on other sites More sharing options...
Badja Posted November 30, 2010 Author Share Posted November 30, 2010 Thank you, you have really helped! Quote Link to comment https://forums.phpfreaks.com/topic/220154-random-number-generator-with-a-twist/#findComment-1141413 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.