Jump to content

array_rand, not really random... is there a better way to grab a random line?


inVINCEable

Recommended Posts

Hey everyone,

 

Ok, So I have a program that generates random paragraphs of sentences arranged in files. Each file has about 30 sentences, and I am generating a random sentence from each file. The only problem is, it does not appear to be random at all. After about 15 random paragraph generations, I get the EXACT same order as one previously generated. I must be doing something wrong because the probability of this happening is about 1:9,000,000,000,000, and it has happened two days in a row, about six times. Please take a look at this simple script and offer some advice, it would be greatly appreciated. Perhaps I am just not seeding the random number generator properly?

 

Who knows, thanks again!

 

<html>

<head>


<?php
//EDIT BELOW

$current_name = "Linda"; 
$postingemail = "linda.clark+";


$title = array_map("trim", file("title.txt"));
$titlepost = $title[array_rand($title)];

$intro = array_map("trim", file("intro.txt"));
$intropost = $intro[array_rand($intro)];

$token1 = array_map("trim", file("token1.txt"));
$token1post = $token1[array_rand($token1)];

$token2 = array_map("trim", file("token2.txt"));
$token2post = $token2[array_rand($token2)];

$token3 = array_map("trim", file("token3.txt"));
$token3post = $token3[array_rand($token3)];

//
//here is where we start the custom stuff that was not included
//
$token4 = array_map("trim", file("token4.txt"));
$token4post = $token4[array_rand($token4)];

$token5 = array_map("trim", file("token5.txt"));
$token5post = $token5[array_rand($token5)];

$token6 = array_map("trim", file("token6.txt"));
$token6post = $token6[array_rand($token6)];

$token7 = array_map("trim", file("token7.txt"));
$token7post = $token7[array_rand($token7)];

$token8 = array_map("trim", file("token8.txt"));
$token8post = $token8[array_rand($token8)];

$token9 = array_map("trim", file("token9.txt"));
$token9post = $token9[array_rand($token9)];

$token11 = array_map("trim", file("token11.txt"));
$token11post = $token11[array_rand($token11)];

$token12 = array_map("trim", file("token12.txt"));
$token12post = $token12[array_rand($token12)];

$token13 = array_map("trim", file("token13.txt"));
$token13post = $token13[array_rand($token13)];

$token14 = array_map("trim", file("token14.txt"));
$token14post = $token14[array_rand($token14)];

$token15 = array_map("trim", file("token15.txt"));
$token15post = $token15[array_rand($token15)];

$token16 = array_map("trim", file("token16.txt"));
$token16post = $token16[array_rand($token16)];

$token17 = array_map("trim", file("token17.txt"));
$token17post = $token17[array_rand($token17)];



$email = array_map("trim", file("email.txt"));
$emailpost = $email[array_rand($email)];
$emailpost = str_replace("%name%", $current_name, $emailpost); 


$price = array_map("trim", file("price.txt"));
$pricepost = $price[array_rand($price)];

//EDIT ABOVE

$number[]="1";
$number[]="2";
$number[]="3";
$number[]="4";
$number[]="5";
$number[]="6";
$number[]="7";
$number[]="8";
$number[]="9";

$numberpost = $number[array_rand($number)];
$numberpost1 = $number[array_rand($number)];
$numberpost2 = $number[array_rand($number)];
$numberpost3 = $number[array_rand($number)];
$numberpost4 = $number[array_rand($number)];

?>

<style>
body {
background-color:#000000; 
color:white;
}
</style>
</head>

<body>

<center>
<br><form><input type=button value="REFRESH" onClick="window.location.reload()">
  <br>
</form> 
<h2>CL Account </h2>
<textarea name="textarea_head" id="textarea" cols="100" rows="1" onClick="this.focus(); this.select();">
<?php echo $postingemail; ?><?php echo $numberpost?><?php echo $numberpost1?><?php echo $numberpost2?><?php echo $numberpost3?><?php echo $numberpost4?>@gmail.com
</textarea>

<br>

<h2>Ad Title </h2>
<textarea name="textarea_head" id="textarea" cols="100" rows="1" onClick="this.focus(); this.select();">
<?php echo $titlepost?>
</textarea>
<br><br>

<h2>Price </h2>
<textarea name="textarea_head" id="textarea" cols="100" rows="1" onClick="this.focus(); this.select();">
<?php echo $pricepost?>
</textarea>
<br><br>

<h2>Ad Body</h2>
<textarea name="textarea_head" id="textarea" cols="100" rows="20" onClick="this.focus(); this.select();">
<?php echo $intropost?> <?php echo $token1post?> <?php echo $token2post?>
<br><br>
<b><?php echo $token3post?></b><br>
- <?php echo $token6post?><br>
- <?php echo $token7post?><br>
- <?php echo $token8post?><br>
- <?php echo $token9post?><br>
- <?php echo $token11post?><br>
- <?php echo $token12post?><br>
- <?php echo $token13post?><br>
- <?php echo $token14post?><br>
- <?php echo $token4post?><br><br>

<u><?php echo $token17post?></u><br>
- <?php echo $token16post?><br>
- <?php echo $token15post?><br>
- <?php echo $token5post?><br>


<br>
<?php echo $emailpost?>

</textarea>
</body>

Link to comment
Share on other sites

From PHP.NET

Reinoud Elhorst

10-Apr-2007 10:50

Please note that (at least in PHP 4.3.11), while each key from the source array has a equal chance of being picked, the order in which they are returned is NOT random. In an array with keys from one to 10, where 2 random keys are picked, 0 is picked as the first number 18.8% of the time, and only as second number in 1.1% of the time:

 

$n=100000;

$a=array(1,2,3,4,5,6,7,8,9,10);

$b=array();

for($i=0;$i<sizeof($a);$i++) {

  $b[$i]=array(0,0);

}

for($i=0;$i<$n;$i++) {

  $keys=array_rand($a,2);

  $b[$keys[0]][0]++;

  $b[$keys[1]][1]++;

}

for ($i=0;$i<sizeof($b);$i++){

  printf("%d: %04.1f%% %04.1f%% %04.1f%%", $i, ($b[$i][0]/$n*100),($b[$i][1]/$n*100),($b[$i][0]+$b[$i][1])/$n*100);

}

 

The result is:

0: 18.8% 01.1% 19.9%

1: 17.0% 03.3% 20.3%

2: 14.3% 05.7% 20.0%

3: 12.2% 07.9% 20.1%

4: 10.0% 10.0% 20.0%

5: 07.8% 12.0% 19.8%

6: 05.5% 14.5% 20.0%

7: 03.3% 16.6% 19.9%

8: 01.2% 18.9% 20.1%

9: 10.0% 09.9% 19.9%

 

The workaround is adding a shuffle command to shuffle the keys:

 

0: 10.0% 10.0% 20.0%

1: 10.0% 10.0% 20.0%

2: 10.0% 10.0% 20.0%

3: 10.0% 10.0% 20.0%

4: 10.0% 10.0% 20.0%

5: 10.0% 10.0% 20.0%

6: 10.1% 10.0% 20.1%

7: 10.0% 10.0% 20.0%

8: 10.0% 10.0% 20.0%

9: 10.0% 10.0% 20.0%

Link to comment
Share on other sites

Try shuffle();

 

I don't really know all that much about it, but I don't think the algorithms that create the "random" results are ACTUALLY random. I think its more "complexly shuffled around", to try and simulate a random result. Because actually, I don't think ANYTHING is truly random. Not even the flip of a coin. It can be controlled under certain circumstances. Therefore, I would presume that after so long, you would receive similar/the same results if your actual count your randomizing isn't changing.

 

Again, just something I theorized. Don't know if it's true or not.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.