求将这段PHP代码转换成C#代码 20
<?phpclassAliasMethod{private$length;private$prob_arr;private$alias;publicfunction__c...
<?php
class AliasMethod
{
private $length;
private $prob_arr;
private $alias;
public function __construct ($pdf)
{
$this->length = 0;
$this->prob_arr = $this->alias = array();
$this->_init($pdf);
}
private function _init($pdf)
{
$this->length = count($pdf);
if($this->length == 0)
die("pdf is empty");
if(array_sum($pdf) != 1.0)
die("pdf sum not equal 1, sum:".array_sum($pdf));
$small = $large = array();
for ($i=0; $i < $this->length; $i++)
{
$pdf[$i] *= $this->length;
if($pdf[$i] < 1.0)
$small[] = $i;
else
$large[] = $i;
}
while (count($small) != 0 && count($large) != 0)
{
$s_index = array_shift($small);
$l_index = array_shift($large);
$this->prob_arr[$s_index] = $pdf[$s_index];
$this->alias[$s_index] = $l_index;
$pdf[$l_index] -= 1.0 - $pdf[$s_index];
if($pdf[$l_index] < 1.0)
$small[] = $l_index;
else
$large[] = $l_index;
}
while(!empty($small))
$this->prob_arr[array_shift($small)] = 1.0;
while (!empty($large))
$this->prob_arr[array_shift($large)] = 1.0;
}
public function next_rand()
{
$column = mt_rand(0, $this->length - 1);
return mt_rand() / mt_getrandmax() < $this->prob_arr[$column] ? $column : $this->alias[$column];
}
}
?> 展开
class AliasMethod
{
private $length;
private $prob_arr;
private $alias;
public function __construct ($pdf)
{
$this->length = 0;
$this->prob_arr = $this->alias = array();
$this->_init($pdf);
}
private function _init($pdf)
{
$this->length = count($pdf);
if($this->length == 0)
die("pdf is empty");
if(array_sum($pdf) != 1.0)
die("pdf sum not equal 1, sum:".array_sum($pdf));
$small = $large = array();
for ($i=0; $i < $this->length; $i++)
{
$pdf[$i] *= $this->length;
if($pdf[$i] < 1.0)
$small[] = $i;
else
$large[] = $i;
}
while (count($small) != 0 && count($large) != 0)
{
$s_index = array_shift($small);
$l_index = array_shift($large);
$this->prob_arr[$s_index] = $pdf[$s_index];
$this->alias[$s_index] = $l_index;
$pdf[$l_index] -= 1.0 - $pdf[$s_index];
if($pdf[$l_index] < 1.0)
$small[] = $l_index;
else
$large[] = $l_index;
}
while(!empty($small))
$this->prob_arr[array_shift($small)] = 1.0;
while (!empty($large))
$this->prob_arr[array_shift($large)] = 1.0;
}
public function next_rand()
{
$column = mt_rand(0, $this->length - 1);
return mt_rand() / mt_getrandmax() < $this->prob_arr[$column] ? $column : $this->alias[$column];
}
}
?> 展开
2个回答
2015-07-26
展开全部
<bean id="CollectionBean" class="test.CollectionBean" parent="abstractCollectionBean">
public class Springusercontroller extends AbstractCommandController{
private String page;
private String error;
private IuserinfoService service;
public class Springusercontroller extends AbstractCommandController{
private String page;
private String error;
private IuserinfoService service;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询