Jump to content

mysql, cronjobs and random numbers


KingChaos

Recommended Posts

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.  8)

 

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  ;D

 

Link to comment
Share on other sites

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

 

 

 

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.