You must Sign In to post a response.
  • Category: [Competition Entries]

    How to generate random numbers?

    how to input random numbers from 1 to 100 numbers randomely without repeating?
  • #707760
    Hi,

    Please check below code,

    Random ranNum = new Random();
    int NewRanNum = ranNum.Next(0, 100);


    Regards,
    Asheej T K

  • #707761
    there will not be any guarantee that random will produce distinct numbers. better is to save it in a list and check for duplicates.
    for example the below code tries to generate 100 random numbers,
    the distinct value will be not 100.



    private void button1_Click(object sender, EventArgs e)
    {
    Random rand = new Random();

    int randNum = 0;
    List<int> ranList = new List<int>();
    for (int i = 1; i < 101;i++ )
    {
    randNum = rand.Next(i, 100);
    if (!ranList.Contains(randNum))
    {
    ranList.Add(randNum);
    }
    }
    //use the random numbers from the list
    ranList.Sort();


    }

    Regards,
    Shine

  • #707875
    hello
    first create a obj for Random class,if u want that value is not repeat then store all value in arraylist using any loop ,i have provide a code ,i hope that helpful for u

    Random random = new Random();
    int count,num;
    ArrayList al = new ArrayList();

    for (i = 0; i <= 99; i++)
    {
    count = 0;
    num = random.Next(1, 100);
    foreach (int n in al)
    {
    if (n == num)
    {
    count++;
    i = i - 1;
    break;
    }
    }
    if (count == 0)
    {
    al.Add(num);
    }

  • #708861
    Thank u, it helps me a lot.

  • #718334
    For generate random number in .vet can use the following code:

    Dim randNumber As New Random
    Dim nunber(100) As Integer
    Dim i As Short
    For i = 1 To 100
    nunber(i) = randNumber.Next(1, 100)
    Next

    From the above code you able to generate 100 random number which will be store in number array.
    the generate number value will be between 1 to 100. if you want to change it change at randNumber.Next(minimum value, maximum value)
    put your desire value in which range you want to generate random number.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.