0

how to Update table column with random numbers using linq in asp.net. Please take a look at my code. same number is updated for all records in the list. what is going wrong in my code.

foreach(var item in list)
            {
                int rnd = 0;
                //item.RandomNumber = GetRandomNumber();
                Application app = new Application ();

                app.Id = item.Id;
                 rnd = GetRandomNumber();
                app.RandomNumber = rnd;
                listapp.Add(app);
            }

public int GetRandomNumber()
{
    int Random = 0;

    Random random = new Random();
    Random = random.Next(1, 99999);
    return Random;
}

It should update unique number for each list. but now column is updated with same number.

1 Answer 1

1

Define the Random object outside of the foreach loop. When you create it on each iteration of the loop, it is likely being seeded with the same value each time, which gives you the same "random" number each time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.