Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

random even number

Options
  • 23-04-2012 11:43am
    #1
    Registered Users Posts: 266 ✭✭


    Hi
    I'm trying very hard to get a random even number from either Math.random() or the nextInt() methods but I cant seem to get anywhere.

    I have resorted to to getting a random number from a group of 5 even numbers but still this does not work...

    int num2=(int)(Math.random()*10)%25+1;
    int[] numbers = new int[]{2,4,6,8,10};

    for(int i=0; i < numbers.length; i++)
    {
    if (numbers == num2)
    //if (numbers%2 == 0 && num2 == numbers) I also tried this
    {
    System.out.println(num2 + " is even number.");
    }
    if (numbers != num2)
    {
    System.out.println(num2 + " is odd number.");
    }
    }

    Any ideas on this would help alot and be much appreciated.
    Thanx

    Ger


Comments

  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Gerb68 wrote: »
    Hi
    I'm trying very hard to get a random even number from either Math.random() or the nextInt() methods but I cant seem to get anywhere.

    I have resorted to to getting a random number from a group of 5 even numbers but still this does not work...

    int num2=(int)(Math.random()*10)%25+1;
    int[] numbers = new int[]{2,4,6,8,10};

    for(int i=0; i < numbers.length; i++)
    {
    if (numbers == num2)
    //if (numbers%2 == 0 && num2 == numbers) I also tried this
    {
    System.out.println(num2 + " is even number.");
    }
    if (numbers != num2)
    {
    System.out.println(num2 + " is odd number.");
    }
    }

    Any ideas on this would help alot and be much appreciated.
    Thanx

    Ger

    Here's how you can do it in .NET C#. Minimum number is 2 and Max is 1000.
    Random random = new Random();
    int randomNumber =1;
    while(randomNumber % 2 != 0)
    {
    randomNumber = random.Next(2,1000);
    }
    //randomNumber is even here
    


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Just get random int and divide by 2.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    srsly78 wrote: »
    Just get random int and divide by 2.

    Which is essentially what I've done inside a While loop so that it keeps getting a new random int until the modulus 2 is even.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Oops I meant multiply by 2 :)


  • Registered Users Posts: 138 ✭✭MagicRon


    John_Mc wrote: »
    Here's how you can do it in .NET C#. Minimum number is 2 and Max is 1000.
    Random random = new Random();
    int randomNumber =1;
    while(randomNumber % 2 != 0)
    {
    randomNumber = random.Next(2,1000);
    }
    //randomNumber is even here
    


    "Infinite" Loop Alert... ;)


  • Advertisement
  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    If number != even, add 1


  • Registered Users Posts: 1,645 ✭✭✭k.p.h


    Get number

    If (Number % 2 > 0 )

    Number + 1 = even number

    Mod operator gives the remainder,As John_Mc has shown if the remainder is greater than 0 the number is not even. Use that as a test until you get the number you want.


  • Registered Users Posts: 138 ✭✭MagicRon


    Giblet wrote: »
    If number != even, add 1

    if number != even, return 4;

    :)


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Guaranteed random, every time.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    int num2=(int)(Math.random()*10)%25+1;

    Not sure what you are trying to do with the mod 25 here. Math.random picks numbers equal or greater than 0, and less than 1.

    So
    int num=(int)(Math.random()*5)*2;
    gives numbers 0,2,4,6,8. Add another 2 to it if you want 2,4,6,8,10.


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    You don't need any while loop

    (Set of all integers) * 2 === (Set of all even numbers)


  • Registered Users Posts: 1,645 ✭✭✭k.p.h


    seamus wrote: »
    You don't need any while loop

    (Set of all integers) * 2 === (Set of all even numbers)

    Neat..


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    k.p.h wrote: »
    Neat..
    Yes, one of the few things I remember from set theory because it's cool.

    Actualy srsly78 got there before me ;)

    To flesh it out a little more, if the OP is looking to get a random number between 2 and 10 (inclusive), then all you have to do is get a random number between 1 and 5 (inclusive) and multiply it by 2.

    The Math.random() function returns a random floating-point number between 0 and 1 (but not zero or 1). The typical way to turn this into an integer is to multiply the number by 10 (this makes it a random floating-point number between 0 and 9), then use Math.floor() to "chop off" the digits after the decimal point.

    So this is

    Math.floor(Math.random() * 10)

    So now we have a random number between 0 and 9. But we need five digits, not ten.

    So now we can use modulus to "reduce" it to a number lower than 5. Or simply % 5. If the number is 9, we now have 4. If the number is 2, it stays 2.

    So
    Math.floor(Math.random() * 10) % 5

    gives us a random number between 0 and 4. But we want between 1 & 5. So we add one. Then to get between 2 and 10, we multiply by two.

    So the total line is

    (((Math.floor(Math.random() * 10) % 5) + 1) * 2)

    You can also multiply the 2 in, and get ((Math.floor(Math.random() * 20) % 10) + 2) but for the sake of documenting a piece of code, I would stick with the first one


  • Registered Users Posts: 2,426 ✭✭✭ressem


    (but not zero or 1)

    Zero is allowed. Something of the order of 1 in 10^14 chance. i.e every Murphy's law day.
    (((Math.floor(Math.random() * 10) % 5) + 1) * 2)

    or
    =2+(int)(Math.random()*5)*2;

    The (int) cast does the truncation, and is above multiplication on the precedence table. So 2+ [0-4] * 2

    Or is there something I'm missing?


  • Closed Accounts Posts: 2,207 ✭✭✭longhalloween


    Why don't you try this?

    #include <stdio.h>
    #include <time.h>

    int main (){
    srand((unsigned)time(NULL));

    int randomNum;
    int check = 0x0001;
    randomNum = rand()%1000 + 2;
    if (randomNum & check ==1)
    randomNum +=1;


    printf("%d", randomNum);

    system("pause");
    return 0 ;

    }

    Basically the random number is ANDed with 0x0001, if the last digit is an odd number, 1 is added, if it's even, it's just printed out.


  • Closed Accounts Posts: 695 ✭✭✭yawha


    Or just set the value to itself XORed with 1.


Advertisement