Wednesday, September 5, 2012

Re: [android-developers] Re: How to generate random numbers between 1 to 8?

Hi, how about this:

public int getMyNumber()
{
    int min = 1;
int max = 8;


Random r = new Random();
int i = r.nextInt(max - min + 1) + min
    switch(i)
    {
    case >= 5 && < 8: return getMyNumber()
    default: return i;
    }

Why in the world would you use recursion to solve this simple problem... Not only that, but theoretically (probably not in practice) you could end up in an infinite or a really long recursion loop...

Bob's way was much nicer and cleaner.
Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Thu, Aug 30, 2012 at 4:40 AM, AntiBit <antonio.bitounis@gmail.com> wrote:
Hi, how about this:

public int getMyNumber()
{
    int min = 1;
int max = 8;


Random r = new Random();
int i = r.nextInt(max - min + 1) + min
    switch(i)
    {
    case >= 5 && < 8: return getMyNumber()
    default: return i;
    }

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment