C#随机始终相同的数字

时间:2012-04-02 18:01:06

标签: c# android mono

public class ExpAdapter : BaseExpandableListAdapter
{
  private int seed = 1000;

  public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.inspection_row_2, null);
        }

        int currentID;
        Random randomizer = new Random(seed);

        TextView question = (TextView) convertView.FindViewById(Resource.Id.questionText);
        RadioGroup radio = (RadioGroup) convertView.FindViewWithTag("actionGroup");
        currentID = randomizer.Next(1,101);
        radio.Id = currentID;
        RadioButton pass = (RadioButton) convertView.FindViewWithTag("passed");
        currentID = randomizer.Next(1,101);
        pass.Id = currentID;
        RadioButton fail = (RadioButton) convertView.FindViewWithTag("failed");
        currentID = randomizer.Next(1,101);
        fail.Id = currentID;
        RadioButton correct = (RadioButton)convertView.FindViewWithTag("corrected");
        currentID = randomizer.Next(1,101);
        correct.Id = currentID;
        RadioButton na = (RadioButton)convertView.FindViewWithTag("na");
        currentID = randomizer.Next(1,101);
        na.Id = currentID;

        string[][] items = questions.childItems();
        question.Text = items[groupPosition][childPosition];

        seed++;

        return convertView;
    }
}

使用断点逐步执行上述操作,currentID = 1。当MonoDroid在父组下创建子控件组并且currentID为ALWAYS = 1时,上述方法被重复调用。我还尝试在整个类中创建currentID和randomizer作为私有静态变量。结果相同。

编辑:我尝试创建新的Random实例,其种子值对于每次调用GetChildView都有所不同。代码在上面编辑,以显示我是如何做到的。 currentID的值仍然是= 1.以下是本地随机发生器信息的屏幕截图。无论种子数是多少,此信息看起来都相同。 ???

enter image description here

1 个答案:

答案 0 :(得分:0)

Randomizer以系统时间(毫秒)计算。因此,如果你快速调用它,即在相同的毫秒内,它们将被播种相同的数字,从而产生相同的随机数。