一个关于超长的不重复随机序列的有关问题

WBOY
Release: 2016-06-13 13:44:27
Original
812 people have browsed it

一个关于超长的不重复随机序列的问题
匿写一个在10000000~90000000范围内生成一个长大100000项的不重复序列

$seedarray =microtime(); 
$seedstr =split(" ",$seedarray,5); 
$seed =$seedstr[0]*10000; 
srand($seed);
$numberarr= array();
for($i=0;$i $newnumber=rand(0,80000000)+10000000;

if(!in_array($newnumber,$numberarr)){
array_push($numberarr,$newnumber);
$i++;
echo $i,'
';
}


}
?>

最终程序到32202处APACHE死机。。。不知道是为什么。。

C# 写类似算法

 private void button1_Click(object sender, EventArgs e)
  {
  Thread t = new Thread(tt);
  t.IsBackground = true;
  t.Start();
   
  }
  private void tt() {
  for (int i = 0; i   {
  string temp = r.Next(0, 80000000).ToString(); ;
  if (!l.Contains(temp))
  {
  l.Add(temp);
  i++;
   
  }
   

  }
  MessageBox.Show("0k");
  }

则没有问题

但是跨线程访问label用以监控变量i时,C#的label也会发生异常。。
不知道是我哪里做错了,还是有其他解决方案?

------解决方案--------------------
试跑了一下,i路到了31408,呵呵。
------解决方案--------------------
php中int的最大值为32769。可能是溢出的缘故吧。

PHP code

<?php $seedarray =microtime(); 
$seedstr =split(" ",$seedarray,5); 
$seed =$seedstr[0]*10000; 
srand($seed); 
$numberarr= array(); 

for($i="1"; $i !== "100000"; ){ 
    //$newnumber=rand(0,80000000)+10000000; 
    $temparr = array();
    for($j=0; $j < 8; $j++){
    $temparr[$j] = ($j == 0) ? rand(0, 7) : rand(0, 9);
    }
    $tempstr = implode($temparr);
    $newnumber = bcadd($tempstr, "10000000");

    if(!in_array($newnumber,$numberarr)){ 
    array_push($numberarr,$newnumber); 
    $i = bcadd($i, "1");
    echo $i.' <br>'; 
    } 
} 
?>
<br><font color="#e78608">------解决方案--------------------</font><br><?php <br />
Copy after login
PHP code
ini_set("max_execution_time", 600);
$seedarray =microtime();
$seedstr =split(" ",$seedarray,5);
$seed =$seedstr[0]*10000;
srand($seed);
$numberarr= array();
for($i=0;$i ';
        if ($i%1000==0)
            flush();
    }   
}
?> <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!