#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

let \$array is having three elements
i want to get those three elements randomly each time but it should be a balanced output... after 9th refresh of page... all elements should be outputted 3 times...

Got what im saying?? can you help me?

##### Share on other sites
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];
?>

##### 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!

##### Share on other sites
Well, for some reasons that I don't understand on my computer I can't use it for \$n>9, I have also fogotten to write an \$aux=0 before that while...

##### 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?

##### Share on other sites
If you need things evened out, then random is never going to do it. Better to just loop the array, so you get a new value for every page refresh.

[code=php:0]\$array = array ( 1, 2, 3 );

\$next = array_shift ( \$array );

array_push ( \$array, \$next );[/code]

me!

##### 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]

##### 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;
}
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...

##### 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!

##### Share on other sites
I checked your script. It doesn't comply with your specification though. Where is the 'random' element?
That's a lot of code just to count down 5-4-3-2-1 then start again.