KingChaos Posted October 3, 2007 Share Posted October 3, 2007 I'm sure there is a post in here somewhere that might help me with this, but I've tried searching, and now I'm even more confused. So here is what I'm trying to do. I got a database with tables etc. And I would like some fields to change the numbers inside them randomly, but with some margins. eg: the lowest numbers in the tables should be e.g 10 and the highest should be e.g 1000. Then, with a cronjob, the numbers shuld change randomly every time the cron runs, but never more than e.g.10 digits up or down on each cron job. (so it can't go from say 15 to 95 in one cronjob) Can this be done with a simple mysql_query like this one wich just add 2% to the number? mysql_query("UPDATE tablename SET field=field+(field*0.02) WHERE field>0;"); Hope someone can help me with this. And remember: I'm not very into this, so please answer so I can understand it Quote Link to comment https://forums.phpfreaks.com/topic/71731-mysql-cronjobs-and-random-numbers/ Share on other sites More sharing options...
KingChaos Posted October 4, 2007 Author Share Posted October 4, 2007 Well.. No answers here so I figured I hade to find out this myself. And I did. And here it is if someone can use it. It might need some fine-tuning, but I hope you guys can tell me if it needs... This is made for running as a cronjob to update a database. First I generates some random numers: $rand1=rand(0, 20); $rand2=rand(0, 20); Theres 2 of them becouse I want 2 change 2 different coloms in my table with differnt random numbers. Then I call the sql server 2 update the table: mysql_query("UPDATE tablename SET rowname=rowname+($rand1)-10 WHERE rowname>0;"); mysql_query("UPDATE tablename SET rowname=rowname+($rand2)-10 WHERE rowname>0;"); I use the -10 to make the random numers to be able to be negative. So here you change your databese entry with -10 to +10. King Quote Link to comment https://forums.phpfreaks.com/topic/71731-mysql-cronjobs-and-random-numbers/#findComment-362009 Share on other sites More sharing options...
fenway Posted October 6, 2007 Share Posted October 6, 2007 Mysql has a rand function too... Quote Link to comment https://forums.phpfreaks.com/topic/71731-mysql-cronjobs-and-random-numbers/#findComment-363261 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.