views:

281

answers:

4

Hi,

I want to generate a random number of 14 positive digits only and i use the below function for it:

public void random()
{
  Random number = new Random();
  long l = number.nextLong();
  number.setSeed(System.currentTimeMillis());    

  long num = Math.abs(number.nextInt())%999 + (l/100000); // problematic line
  mTextBox.setString("" + num);
}

I very new to to JavaMe, i have made above funtion myself but i believe it is not working as expected. It also generates -ve numbers. Also sometimes one or two digits are missing in generated number resulting in 12 or 13 numbers not 14.

Any suggestions or improvement to the code will be highly appreciated. Thansk :)

+2  A: 

If you want 14 digits, then you should use 14 calls to number.nextInt(10) - something like this:

public static String randomDigits(Random random, int length)
{
    char[] digits = new char[length];
    // Make sure the leading digit isn't 0.
    digits[0] = (char)('1' + random.nextInt(9);
    for (int i = 1; i < length; i++)
    {
        digits[i] = (char)('0' + random.nextInt(10));
    }
    return new String(digits);
}

Note that I've made the instance of Random something you pass in, rather than created by the method - this makes it easier to use one instance and avoid duplicate seeding. It's also more general purpose, as it separates the "use the string in the UI" aspect from the "generate a random string of digits".

I don't know whether Random.nextInt(int) is supported on J2ME - let me know if it's not. Using Math.abs(number.nextInt())%999 is a bad idea in terms of random distributions.

Jon Skeet
Shouldn't the first digit be between 1 and 9?
UncleO
@uncleo: I've mentioned this in an edit - it depends on whether it's meant to be treated as a *number* or just a sequence of 14 digits. It's easy enough to fix if necessary.
Jon Skeet
@Steet: Thanks nextInt is supported there. As i said i am new to this, can you please provide a fix for number between 1 and 9. Also 0 should not be there in the beginning. Finally, will that only create positive number? Thanks
Sarfraz
@Sarfraz: We're only generating digits, so how could it possibly generate a negative number? Will edit for avoiding the leading zero.
Jon Skeet
@Skeet: Thanks man for your help, i have implemented it, it is working fine :)
Sarfraz
+1  A: 
long num = 10000000000000L+(long)(random.nextDouble()*90000000000000.0);

EDIT:

mTextBox.setString(MessageFormat.format("{0,number,00000000000000}",
    new Object[] {new Long(num)}));
Maurice Perry
There's absolutely no guarantee that'll produce a 14 digit number. What if nextDouble returns 0?
Jon Skeet
I think nextDouble is not supported in j2me
Sarfraz
@Jon Skeet: this is a formatting problem, @Sarfraz Ahmed: it is in CLDC 1.1
Maurice Perry
@Maurice: At that point you've got the same problem as my original answer did, of having leading zeroes. I would also be somewhat concerned about the distribution of the later digits. It just seems a fairly roundabout way of doing it - Sarfraz wants 14 digits, so why not just generate 14 digits?
Jon Skeet
Arlight then, why not
Maurice Perry
+2  A: 

I didn't understand what you really want, the code suggests that you want a 3 digit number (%999).
Otherwise you can create a 14 digit number between 1000000000000000 and 9999999999999999 by

long num = 1000000000000000L + (long)(number.nextDouble() * 8999999999999999.0);


note (1 / 100000) is 0 (zero) since it is done by integer division, use (1.0 / 100000.0) for double division

Carlos Heuberger
+1  A: 

You are getting negative numbers because Random.nextInt() returns any 32-bit integer, and half of them are negative. If you want to get only positive numbers, you should use the expression Random.nextInt() & 0x7fffffff or simply Random.nextInt(10) for a digit.

Roland Illig