# Thread: How To Do This Random Algorithym?

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

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

4. It needs to be based on what I wrote.

5. 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. 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. One question ethernet where is the random chosen ID?

8. Summit your code did not work sorry... didn't display ANYTHING.

9. Actually Ethernet it worked thank you dude so much.

10. 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(\$handle, MYSQL_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>0 && (\$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> ```

#### Posting Permissions

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