Jump to content

Rand() help needed


herghost

Recommended Posts

Hi all,

 

Can someone explain and give me a quick example of how I would go about this?

 

I have 100 values in a database with  colum headings of t_val and t_name. If I wanted to update all 100 of these with a rand number how would I do this?

 

Would I do something like this?

t_val1 = rand(1,10);

 

What I want to do is have different variances in the rand as well. So ten would be rand(1,10) and the next ten would be rand(5,20) for example?

 

Many Thanks

Link to comment
Share on other sites

Just re-read that and I am not sure how much sense it made so I will try again.

 

I have a table in my database that looks like this:

 

stock_id stock_name stock_desc stock_code t_val y_val

 

 

Now there is 100 rows in this table, with stock_id running from 5 to 104.

 

What I am trying to achieve is to update all these with a random value for t_val, while leaving the rest of the data intact.

 

I cant seem to get my head around how I would do this!

 

 

Link to comment
Share on other sites

I would pull the data first, then push the random number

 

this is the short version, since I don't know your mysql interface.

$array = "SELECT id FROM table";

for($i = 0; $i < count($array); $i++){
   $update = "UPDATE table SET t_val='" . rand(1,10) . "' WHERE id='" . $array[$i] ."'";
}

 

**EDIT **

this will NOT ensure that each entry gets a different random number, if that is needed.

Link to comment
Share on other sites

Would it be possible to do something like this?

 

Grab rows 5 to 15

enter into array value of t_val for these rows

count number of values

generate rand(1,10) for each value counted

replace values with rand output

update rows 5 to 15

 

Hmm, logically this makes sense to me, however its not within my skills in php even to know if its possible!

 

Link to comment
Share on other sites

I've actually been working on an incrementing system for you.  Hopefully, something like this will do the job:

<?php
$array = "SELECT id FROM table";
$res = mysql_query($array);
for($i = 0; $i < count($res); $i++){
if (is_int($i/10) || $i == 0){
	$rand_low = rand(1,5);
	$rand_high = rand(10,20);
}
$num = rand($rand_low,$rand_high);
   $update = "UPDATE table SET t_val=$num WHERE id='" . $res[$i] ."'";
}

Link to comment
Share on other sites

Many many thanks for your efforts :)

 

include('../common/dbconnect.php');

$array = "SELECT stock_id FROM stocks";
$res = mysql_query($array);
for($i = 0; $i < count($res); $i++){
   if (is_int($i/10) || $i == 0){
      $rand_low = rand(1,5);
      $rand_high = rand(10,20);
   }
   $num = rand($rand_low,$rand_high);
   $update = "UPDATE stocks SET t_val=$num WHERE stock_id='" . $res[$i] ."'";
}

 

This is what I am running in my browser to match up with my database details, however nothing happens! No errors and no update in the database? Am I missing something obvious here? This is far more advanced than the stuff I have been doing!

Link to comment
Share on other sites

forgot to add this:

<?php
$array = "SELECT id FROM table";
$res = mysql_query($array);
for($i = 0; $i < count($res); $i++){
if (is_int($i/10) || $i == 0){
	$rand_low = rand(1,5);
	$rand_high = rand(10,20);
}
$num = rand($rand_low,$rand_high);
   $update = "UPDATE table SET t_val=$num WHERE id='" . $array[$i] ."'";
   if (mysql_query($update)){
   	echo "Updated $i record(s)<br />\n";
   }
}

Link to comment
Share on other sites

Try this method:

<?php
$array = "SELECT id FROM table";
$res = mysql_query($array);
$count = 0;
while ($row = mysql_fetch_assoc($res)){
if (is_int($count / 10) || $count == 0){
	$rand_low = rand(1,5);
	$rand_high = rand(10,20);
}
$num = rand($rand_low,$rand_high);
$update = "UPDATE table SET t_val=$num WHERE id='" . $row['id'] ."'";
if (mysql_query($update)){
	echo "Updated $i record(s)<br />\n";
}
}

Change the querries to suit your schema.

Link to comment
Share on other sites

that was broken code I gave you. Sorry. Here's a fix:

<?php
$array = "SELECT id FROM table";
$res = mysql_query($array);
$count = 0;
while ($row = mysql_fetch_assoc($res)){
   if (is_int($count / 10) || $count == 0){
      $rand_low = rand(1,5);
      $rand_high = rand(10,20);
   }
   $num = rand($rand_low,$rand_high);
   $update = "UPDATE table SET t_val=$num WHERE id='" . $row['id'] ."'";
   $count++;
   if (mysql_query($update)){
      echo "Updated $count record(s)<br />\n";
   }
}

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.