1

ランダム メソッドを使用して、5 問のクイズを作成するためにラベルと RadioButtonList アイテムをランダムに入力しようとするタスクのほとんどを達成することができました。このコードは、大きなプールから抽出した質問を適切にブレンドして生成します。

ただし、RadioButtonList 項目をランダムに設定するための内部コードはおかしな動作をしています。

  1. while ループにブレークポイントを挿入して段階的に実行すると、次の質問ごとに異なる順序で回答が入力されます。

  2. しかし、単純にブラウザーを開いて、展開先の SharePoint Web サイトから実行すると、次のすべての質問/すべての質問で回答が同じ順序で表示されます。注: ただし、ページの更新時に一意の注文がスローされます。つまり、クイズを再開したときです。

私は私の人生の問題を理解できないようです。いくつかの目を使用できます。

これまでのコードは次のとおりです。アドバイスしてください

  public void LoadQuestions()
    {
        try
        {
            SPWeb thisWeb = SPContext.Current.Web;
            SPList oSPList = thisWeb.Lists["QuestionsAndAnswers"];
            SPListItemCollection oSPListItemCollection = oSPList.Items;

            Random rand = new Random();
            List<int> tempStore = new List<int>();
            List<int> tempStore2 = new List<int>();
            int tempValue = 0;
            //int tempValue2 = 0;
            int icount = 1;
            int iMax = oSPListItemCollection.Count;
            //int icount2 = 0;
            //int iMax2 = oSPListItemCollection.Count;
            SPListItem thisItem;
            Label thisQuestion;
            Label thisCorrectAnswer;
            RadioButtonList thisAnswers;

            while (icount < 6)
            {
                tempValue = rand.Next(1, iMax);
                if (tempStore.Exists(value => value == tempValue))
                    continue;
                else
                {
                    tempStore.Add(tempValue);
                    thisQuestion = (Label)UpdatePanelMaster.FindControl("Question" + icount.ToString());
                    thisItem = oSPListItemCollection[tempValue];
                    thisQuestion.Text = icount + ". " + thisItem["Question"].ToString();

                    thisCorrectAnswer = (Label)UpdatePanelMaster.FindControl("CorrectAnswer" + icount.ToString());
                    thisCorrectAnswer.Text = thisItem["CorrectAnswer"].ToString();

                    thisAnswers = (RadioButtonList)UpdatePanelMaster.FindControl("RadioButtonList" + icount.ToString());

                    //Entering code to handle random answer arrangements 
                    //This code works ok when run in a step by step debug fashion but no when deployed  and run from the website directly. 
                    Missing/Changes required?
                    int tempValue2;
                    int Icounter = 0;
                    string[] AnswerArr = new string[] { thisItem["CorrectAnswer"].ToString(), thisItem["IncorrectAnswer1"].ToString(), thisItem["IncorrectAnswer2"].ToString(), thisItem["IncorrectAnswer3"].ToString() };
                    Random rand2 = new Random();

                    while (Icounter < 4)
                    {
                        tempValue2 = rand2.Next(0, 13);//max number of items in the array 
                        decimal toCeilingVar = tempValue2 / 4; 
                        int fraction = Convert.ToInt32(System.Math.Ceiling(toCeilingVar));
                        string tempArrValue = AnswerArr[fraction];
                        if (thisAnswers.Items.FindByValue(tempArrValue) == null)
                        {
                            //add new value because the current value is not in the list
                            thisAnswers.Items.Add(tempArrValue);
                            Icounter++;
                            tempArrValue = string.Empty;
                        }
                        tempValue2 = 0;
                        toCeilingVar = 0;
                        fraction = 0; 

                        //End Random Answer handling 
                    }
                    tempValue = 0;
                    icount++;
                }
            }
        }
        //End random question handling

        catch (Exception ex)
        {
            throw ex;
        }
    }
4

2 に答える 2

1

問題は(ループ内)です:

Random rand2 = new Random();

クラスは、Random明示的なシードが提供されていない場合、時間を使用してシードされます。タイトなループで複数のインスタンスを作成すると、多くの場合、それらは十分に (時間的に) 接近しているため、同じシードを持つことになります。これは、それらがすべて疑似乱数の同じシーケンスを生成することを意味します。2 つのオプション:

  • Random内部インスタンス ( ) を完全に失いますrand2- 単一の外部インスタンス ( rand) を使用するだけです。より多くのRandomインスタンスを使用してもランダム性は増加しません
  • 外部インスタンスを使用して内部インスタンスをシードします

並列処理など、正当な理由がある場合にのみ後者を使用します

于 2013-06-27T04:41:33.343 に答える
1

おそらくここで起こっていることは、本番環境で実行されると、ラインRandom rand2 = new Random()が非常に短い間隔で数回呼び出されるため、各インスタンスの初期シード値が同じになることです。私の経験では、Random の既定のコンストラクターは、解像度がかなり低い時間ベースのシードを使用します。

これを解決するには、単一のrand変数を使用してすべての乱数を生成するかrand2、最も外側のループの外で初期化します。

于 2013-06-27T04:45:03.993 に答える