Jump to content

better than rand(); ?? i want to get an array's elements randomly.. but balanced


karthikeyan_coder

Recommended Posts

Include this file I have written for you!

<?php
//$n is your square number (9 if i am not mistaken), use only square numbers!
$n=9;
for ($i=0; $i<round(sqrt($n)); $i++)
{$arra[$i][0]=$i;
$arra[$i][1]=1;}
for ($i=round(sqrt($n)); $i<$n; $i++)
{ $newnr=rand()%round(sqrt($n));
  if  ($arra[$newnr][1]<round(sqrt($n))) {$arra[$i][0]=$newnr; $arra[$newnr][1]++;}
  else
  {while (!$aux)
    {$aux=0;
      $newnr=rand()%round(sqrt($n));
      if ($arra[$newnr][1]<round(sqrt($n))) {$arra[$i][0]=$newnr; $arra[$newnr][1]++; $aux=1;}

    }


    }
}
for ($i=0; $i<$n; $i++)
echo $arra[$i][0];
?>
Link to comment
Share on other sites

Well, the first round(sqrt($n))-1 numbers are not random (the numbers are simply in ascending order) but the rest of them are random (you can modify it to use only the round(sqrt($n)) to $n numbers). But I gave what you needed most! Don't use large numbers for $n, it will significantly slow your program!
Link to comment
Share on other sites

no i mentioned 9... it is just an example... see if we use this techq in banner showcase script then the number of impressions should be balancers... let..
$a[1] = 1;
$a[2] = 2;
$a[3] = 3;
$a[4] = 4;

let us say each value of an array is pointing some other information like... banner id numbers which is placed in mysql table...

if the page refreshes then our script should take one value from array...
1st impression <One array element>
2nd impression <Another array element>
3rd impression <... another....>

if the page got refreshed 20 or 200 times (it should be n number of times) at end of the total impressions... we are going to see the individual impressions of each array elements. it should be balanced like...
$a[1] = [10 impressions]
$a[2] = [10 impressions]
$a[3] = [12 impressions]
$a[4] = [11 impressions]

impressions should be like above....

got what im trying to say?
Link to comment
Share on other sites

Create an array comprising the original array x 3
Shuffle() it.

Now you have 9 random elements with each of the original 3 occuring 3 times

[code]
<?php
$array = array (1, 2, 3);
$b = $array;
$k = count($array)-1;
for ($i=0; $i<$k; $i++) {
    $array = array_merge($array, $b);
}
shuffle ($array);
echo '<pre>', print_r($array, true), '</pre>';
?>[/code]
Link to comment
Share on other sites

ahh Thank you Barand , printf and amalosoul ... i did the following code while waiting for all of your replies.. please check this... :)

[code]<?php
session_start();
mysql_connect("localhost","root","");
mysql_select_db("djnaf");
if(!isset($_SESSION['total']))
{
$num = mysql_num_rows(mysql_query("SELECT * FROM `banners`"));
$_SESSION['total'] = $num;
//echo("rows $num");
}
$num = mysql_num_rows(mysql_query("SELECT * FROM `banners`"));
//echo("rows $num");
$current = $_SESSION['total'];
$query = "SELECT * from `banners` LIMIT 0,$current";
//echo($query);
$pass = mysql_query($query) or die(mysql_error());
$numb = mysql_num_rows($pass);
for($i = 1;$i <=$numb; $i++)
{
$fetch = mysql_fetch_array($pass);
$id = stripslashes($fetch['id']);
$banners[$i] = $id;
//Add values in array.
}
print_r($banners);
ksort($banners);
$end = count($banners);
$arr = array_pop($banners);
echo("Show banner $arr");
if($current != 1)
{
$limit = (int)$current - 1;
$_SESSION['total'] = $limit;
}
else
{
session_destroy();
unset($_SESSION['total']);
}
?>[/code]


SQL
[quote]CREATE TABLE `banners` (
  `id` int(11) NOT NULL,
  `name` text NOT NULL
) TYPE=InnoDB;

--
-- Dumping data for table `banners`
--

INSERT INTO `banners` (`id`, `name`) VALUES (1, 'b1');
INSERT INTO `banners` (`id`, `name`) VALUES (2, 'b2');
INSERT INTO `banners` (`id`, `name`) VALUES (3, 'b3');
INSERT INTO `banners` (`id`, `name`) VALUES (4, 'b4');
INSERT INTO `banners` (`id`, `name`) VALUES (5, 'b5');[/quote]


Plese test this script and tell me how it is :) im testing yours...
Link to comment
Share on other sites

If your doing banners I would think about using a IO handle, using r+ and fseek(), it would be more resource friendly. Sometimes a database is the best thing, but I have found when writing banners rotation script or word based image security scripts that would normally run a query or two, are better served using a simple single IO read/write. I have many banner db(s) (flat files) one has 73,654 different banner (info) that rotate 3 - 5 times a day, being called by 143 different sites and it works wonderful and the server runs 1/5 load that it use to, when I was serving the banner (info) from the db.

me!
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.