Random within a Random

Asked

Viewed 107 times

2

Guys, I have this code here:

<?php
// Define um array com alguns sites:
$site[] = 'google.com';
$site[] = 'facebook.com';


// Escolhe um valor aleatório (respeitando o total de sites)
$destino = rand(0, (count($site) - 1));

// Redireciona o usuario:
header("Location: " . $site[$destino]);
?>

He currently randomly chooses between the two links and opens one of them. But I wanted a simple change, and I don’t know how to do it exactly because I don’t understand much of it. I wanted the second option to have two options. But what do you mean? I wanted something like this?

<?php
// Define um array com alguns sites:
$site[] = 'google.com';
$site[] = 'youtube.com'  ouuu  'facebook.com';


// Escolhe um valor aleatório (respeitando o total de sites)
$destino = rand(0, (count($site) - 1));

// Redireciona o usuario:
header("Location: " . $site[$destino]);
?>

So, an Andom inside an Andom. Someone could help me with this?

  • 2

    And why the facebook.com could not enter as a third item in the array?

  • Because I wanted only about 25% of the hits to this site. Google would be the main redirect, in case about 50% chance. And then this other 50% I wanted to divide between the other two links, so I can’t create a third array, if it wouldn’t be about 33% chance of access in each array

  • 3

    So the question should be this xD "How do you draw a value considering different weights for each value" or something like that.

  • 2

    Simply add another google entry or make an array that puts the weight of each element and see which index was based on the Rand range (:

2 answers

6


What you want is to draw an element from the list with different weights. There is an explanation for this in the question below and it is not worth replicating here:

In PHP, a possible implementation would be:

$urls = [
    'url A' => 50,
    'url B' => 25,
    'url C' => 25
];

$x = rand(0, 100);

foreach ($urls as $url => $probability) {
    $x -= $probability;

    if ($x <= 0) {
        echo $url;
        break;
    }
}

Where URL A has a 50% chance of being drawn, URL B has a 25% chance and URL C has a 25% chance of being drawn. You can change it $urls freely, provided that the sum of the probabilities is always 100, for obvious reasons.

  • 1

    Thanks, man. Thanks a lot!

-1

<?php
$sites = array("siteprincipal");
$site_ind = array("siteindependente1.com", "siteindependente2.com", "siteindependente3.com", "siteindependente4.com");
$sites[1] = $site_ind;
$destino = rand(0, count($sites)-1);
if(is_array($sites[$destino])){
    $destinoind = rand(0, count($sites[$destino])-1);
    header("Location:". $sites[$destino][$destinoind]);
}else{
    header("Location:".$sites[$destino]);
}
?>
  • Could you help me? I don’t know much about these things :/

  • You will only use three sites?

  • They will be 6. Ai wanted type, 4 of them independent, and two within a single array. Like this: $site[] = 'site1.com';&#xA;$site[] = 'site2.com';&#xA;$site[] = 'site3.com';&#xA;$site[] = 'site4.com';&#xA;$site[] = 'site5.com' ouuu 'site6.com';

  • <?php $sites = array("main site"); $site_ind = array("siteindependente1.com", "siteindependente2.com", "siteindependente3.com", "siteindependente4.com"); $sites[1] = $site_ind; $destination = Rand(0, Count($sites)-1); if(is_array($sites[$destination])){ $destinoind = Rand(0, Count($sites[$destination])-1); header("Location:". $sites[$destination][$destinoind]); }Else{ header("Location:".$sites[$destination]); }&#Xa a; ?>

  • You just have to break the lines to stay straight

  • And swap strings inside arrays

  • Then tell me if it worked

  • I followed the example of the other guy up there. But thank you so much for the strength, man.

  • Bruno, you can do it [Edit] your answer and put the code there. It is better because it gets more organized (all the information together, so people don’t have to be "hunting" the code in the comments), and in addition in the answer it is possible to format the code, leaving it much more readable than in the comments

  • Vlw, I’m new to the kk platform

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.