SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How To Do This Random Algorithym?

    Ok I am making a link trading script. I have a database.
    |ID|Name|URL|Hits
    1|Site One|http://www.site1.com|43342
    2|Site Two|http://www.site2.com|9274
    3|Site Three|http://www.site3.com|432904

    I want the script to choose a site to go to based on chances. The chance is based on a percentage.

    So since ID 3's hits are about 89% (using round) of the total it has an 89% chance of being chosen. How would I do this and make it into a script so it counts the total from the database and calculates each percent? I am sorry if this sounds a bit too hard I just can't get it in my head.
    Have a good day.

  2. #2
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add al the hits.
    Create a random number between 0 and your total amount of hits.
    now write code for this logic:
    hitsamount=0
    i=0
    while( random number > hitsamount+hits[i])
    hitsamount+=hits[i++]



    now i will be the index you want

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What?
    Have a good day.

  4. #4
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It needs to be based on what I wrote.
    Have a good day.

  5. #5
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is in pseudo-code,
    hits[0] will be the hits of the first site
    hits[1] will be the hits of the second site...

  6. #6
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    #You need to first connect to your database (I'm assuming it is MySQL)

    #get all the results from your table of sites
    $query "SELECT * FROM $table_name";
    $result mysql_query $query$link );

    #set $x to 1 or the index of the arrays will not reflect the SQL id
    $x 1;

    #loop through all of the results in $table_name
    while ($a_row mysql_fetch_object$result ) ) {

      
    #assign the details to a few arrays
      
    $id[$x] = $a_row->id;
      
    $name[$x] = $a_row->name;
      
    $url[$x] = $a_row->URL;
      
    $hits[$x] = $a_row->hits;

      
    #keep a running total of the number of hits (used later to work out the percentage (also increminate $x)
      
    $total_hits += $a_row->hits;
      
    $x++;
    }

    #set $y to 1 or the results will be off by 1 because $x was set to 1
    $y 1;

    #loop through the arrays to work out the percentage chance.
    while ($y $x) {

      
    $percentage[$y] = $hits[$y] / $total_hits 100;

    }

    #This should leave you with a few arrays with the index being the id in the SQL table
    #If I was better with objects I would have used those, but it is arrays for now :P 
    I think this will work, if not, just tweak it a bit. And I hope I've helped.

  7. #7
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One question ethernet where is the random chosen ID?
    Have a good day.

  8. #8
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Summit your code did not work sorry... didn't display ANYTHING.
    Have a good day.

  9. #9
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually Ethernet it worked thank you dude so much.
    Have a good day.

  10. #10
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's a script that does everything for you.. try it. I don't store the percentages, i just use the amount of hits they've recieved and a random number to figure out which link the random number could be associated with.. I'm sure there are better ways, but this was pretty simple, took a bit of testing to get it working right though.
    PHP Code:
    <?php
    mysql_connect
    ("localhost","your_username""your_password");
    mysql_select_db("links");

    if (isset(
    $_GET['random_page'])) {
        
    $handle mysql_query("SELECT * from link_info");
        
    $links = array();
        
    $total_hits $i =0;
        
    $hits = array();
        while (
    $row mysql_fetch_array($handleMYSQL_ASSOC)) {
            
    $links[$i]['name']= $row['name'];
            
    $links[$i]['url'] = $row['url'];
            
    $total_hits += $row['hits'];
            
    $hits[$i] = $total_hits;
            
    $i++;
        }

        
    //Get a random number between 0-total_hits
        
    $random_number mt_rand(0$total_hits);
        for (
    $i=0; !isset($link) ; $i++) {
            if (
    $i==0) {
                if (
    $random_number <= $hits[$i]) {
                    
    $link $i;
                }
            } elseif (
    $i>&& ($i<count($hits) -1)) {
                 if (
    $random_number $hits[$i-1] && ($random_number <= $hits[$i])) {
                    
    $link $i;
                }
            } else {
                
    $link $i;
            }
        }
        
    $url  $links[$link]['url'];
        
    header("Location:$url");
        exit;
        
    }


    ?>

    <html>
    <head><title>Random Link Test</title>
    <body>
    <p>
    <a href="<?php echo $_SERVER['PHP_SELF'];?>?random_page=go" title="Click Here To Visit A Random Link">Random Link</a>
    </p>
    </body>

    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •