随机应用随机变量


applying a random variable randomly

>我有代码生成随机字体并将新的随机字体应用于每行文本,并想添加一个

$line = str_replace ("a", "@", $line);

但我希望将其应用于每行的可能性为 10%,而不是应用于整个字符串。我该怎么做?这是我现有的代码:

$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma", "Geneva", "Times New Roman");
shuffle($fonts);
$output = "";
$lines = array_slice(file("users.txt"), -20, 20);
$i = 0;
foreach ( $lines as $line ) {
  if($i == count($fonts)) {
    shuffle($fonts);
    $i = 0;
  }
  $output .= '<div style="font-size: ' . rand(15, 23) . 'px; font-family:' . $fonts[$i] . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . "</div>'n";
  $i++;
    }
echo $output;

刚刚开始熟悉php,我觉得我有一个全新的网络可以玩! :D

如果您希望有

10% 的机会将"a"更改为"@",请尝试插入以下代码:

if(rand(0, 9) == 1)
{
    $line = str_replace ("a", "@", $line);
}

它会生成一个从 0 到 9 的整数,如果它等于 1(10% 的机会,那么它会应用您的格式。

所以,在 te foreach($lines as $line) 循环中,添加这个

$rand = rand(0,9);
if($rand == 0)
{
    $line = str_replace("a","@",$line);
}