Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 27 September 2006 - 09:17 PM

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?
www.karthi.us

#2 amalosoul

amalosoul
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 27 September 2006 - 10:13 PM

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];
?>

#3 amalosoul

amalosoul
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 27 September 2006 - 10:20 PM

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!

#4 amalosoul

amalosoul
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 27 September 2006 - 10:44 PM

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

#5 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 27 September 2006 - 11:30 PM

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?

www.karthi.us

#6 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 27 September 2006 - 11:50 PM

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.

$array = array ( 1, 2, 3 );

$next = array_shift ( $array );

array_push ( $array, $next );

me!

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 September 2006 - 12:01 AM

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

<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 28 September 2006 - 12:21 AM

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

<?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']);
}
?>


SQL

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');



Plese test this script and tell me how it is :) im testing yours...

www.karthi.us

#9 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 28 September 2006 - 01:01 AM

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!

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 September 2006 - 08:10 AM

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.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users