none
How to generate two random integers that their multiple is < 100?

    Question

  • How to generate two random integers that their multiple is < 100?

    int no1

    int no2

    //then?

    The method below doesn't work

    -------
     while (no1 * no2 >100)
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(51);
                }
    --------
    Tuesday, June 07, 2011 5:09 AM

Answers

  • The first number selected determines the possible values
    for the second number:

       
    Random r = new Random();
        
    private void button1_Click(object sender, EventArgs e)
        {
          
    int num1 = r.Next(100);
          
    int num2 = r.Next(100 / num1);
          textBox1.Text = num1.ToString() + 
    " " + num2.ToString();
        }

    • Proposed as answer by Louis.fr Tuesday, June 07, 2011 9:56 AM
    • Marked as answer by lucy-liuModerator Tuesday, June 14, 2011 8:29 AM
    Tuesday, June 07, 2011 9:22 AM

All replies

  • Try below code

          Random rand = new Random();
    
          int no1 = 0;
          int no2 = 0;
    
          do
          {
            no1 = rand.Next(50);
            no2 = rand.Next(10);
          }while (no1 * no2 > 100);
    
    


    Thanks,
    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Tuesday, June 07, 2011 5:55 AM
  • thank you so much!

    What makes the two codes different?

    Tuesday, June 07, 2011 6:10 AM
  • Homework?
    Success
    Cor
    Tuesday, June 07, 2011 6:16 AM
  • no

    just learning to use c#

    i'm a noob

    Tuesday, June 07, 2011 6:19 AM
  • The difference is how you test the numbers.

    If you initialize no1 and no2 to numbers above 100 your code will work

    This will always return no1 and no2 with 0

                int no1 = 0, no2 = 0;
     
                while (no1 * no2 > 100)
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(51);
                }
    

    This will work

                int no1 = 0, no2 = 0;
     
                do
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(51);
                }
                while (no1 * no2 > 100);
    

    Or this

                int no1 = 101, no2 = 101;
     
                while (no1 * no2 > 100)
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(51);
                }

    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Tuesday, June 07, 2011 6:20 AM
  • Thank you

    However, surprisingly this works

                int no1 = 101, no2 = 101;
     
                while (no1 * no2 > 100)
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(51);
                }

    while this doesn't

                int no1 = 101, no2 = 101;
     
                while (no1 * no2 > 100)
                {
                    no1 = randomizer.Next(51);
                    no2 = randomizer.Next(); <------------
                }

    two numbers 2 and 137 are generated

    why?

    Tuesday, June 07, 2011 6:45 AM
  • Because by calling Random.Next with no parameters, i.e. randomizer.Next() in your case, you are asking it to generate a random number between 0 and 2,147,483,647 (i.e. Int32.MaxValue).

    Only when you supply an argument to the function call, i.e. randomizer.Next(51), are you specifying your own custom limits.

    See here for the possible Random.Next method signatures and their uses.
    • Edited by Jack0x539 Tuesday, June 07, 2011 7:57 AM Added MSDN link
    Tuesday, June 07, 2011 7:55 AM
  • Hi,

    I don't think it says 137 but something much larger.  Multiplying a small number with a very large number may end up in a number larger than an integer can represent.  In that case you will get an overflow and the result happens to be a number under 100.  

    By default integer arithmetic does not throw overflow exceptions, but you can force them to by using the 'checked' keyword

                int no1 = 101, no2 = 101;
     
                checked
                {
                    while (no1 * no2 > 100)
                    {
                        no1 = randomizer.Next(51);
                        no2 = randomizer.Next();
                    }
                }

    2147483647 + 1 = -2147483648

     


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Tuesday, June 07, 2011 8:06 AM
  • The first number selected determines the possible values
    for the second number:

       
    Random r = new Random();
        
    private void button1_Click(object sender, EventArgs e)
        {
          
    int num1 = r.Next(100);
          
    int num2 = r.Next(100 / num1);
          textBox1.Text = num1.ToString() + 
    " " + num2.ToString();
        }

    • Proposed as answer by Louis.fr Tuesday, June 07, 2011 9:56 AM
    • Marked as answer by lucy-liuModerator Tuesday, June 14, 2011 8:29 AM
    Tuesday, June 07, 2011 9:22 AM
  • do
    
    {
    
      no1 = rand.Next(50);
    
      no2 = rand.Next(10);
    
    }while (no1 * no2 > 100);
    
    

    In theory, that's a potentially infinite loop. JohnWein's code is sure to get the desired result with no loops.


    Tuesday, June 07, 2011 9:58 AM
  • Hi,

    I don't think it says 137 but something much larger.  Multiplying a small number with a very large number may end up in a number larger than an integer can represent.  In that case you will get an overflow and the result happens to be a number under 100.  

    By default integer arithmetic does not throw overflow exceptions, but you can force them to by using the 'checked' keyword

     

                int no1 = 101, no2 = 101;
     
                checked
                {
                    while (no1 * no2 > 100)
                    {
                        no1 = randomizer.Next(51);
                        no2 = randomizer.Next();
                    }
                }

    2147483647 + 1 = -2147483648

     

     

     


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]

    oh,you may be right

    the number "137" looked strange in the program

    Sunday, June 12, 2011 3:31 PM
  • The first number selected determines the possible values
    for the second number:

       
    Random r = new Random();
        
    private void button1_Click(object sender, EventArgs e)
        {
          
    int num1 = r.Next(100);
          
    int num2 = r.Next(100 / num1);
          textBox1.Text = num1.ToString() + 
    " " + num2.ToString();
        }

    great

    thank you : D

    Sunday, June 12, 2011 4:15 PM
  • ya

    Will this shorten the loading time of the program?

    or the loading time of both are just similar?
    Sunday, June 12, 2011 4:17 PM
  • Here is another solution:

        Random r = new Random();
        public Method()
        {      
          int a, b;
          a = r.Next(1, 100);
          b = r.Next(1, 100 - a);
          MessageBox.Show(String.Format("Two randomly generated numbers are: {0}, {1}", a, b));
        }
    



    Mitja
    Sunday, June 12, 2011 4:57 PM
  • Here is another solution:

     

      Random r = new Random();
      public Method()
      {   
       int a, b;
       a = r.Next(1, 100);
       b = r.Next(1, 100 - a);
       MessageBox.Show(String.Format("Two randomly generated numbers are: {0}, {1}", a, b));
      }
    


     


    Mitja

    I don't think so. a = 50, b = 49.
    Sunday, June 12, 2011 5:32 PM
  • Ups, correct, I did a mistake. The multiplication a and b must be lower then 100.

    So yours (John`s) is correct answer. 


    Mitja
    Sunday, June 12, 2011 5:42 PM
  • ya

    Will this shorten the loading time of the program?

    or the loading time of both are just similar?

    No loop is always faster than one loop.
    Wednesday, June 15, 2011 12:34 PM