You Are Here:

Community: Developer Discussion Boards

#1 Old Provide code - 2004-10-07, 05:53

Join Date: Oct 2004
Posts: 9
Jeff_Ang82
Offline
Registered User
Does anyone know how to use random function? What will the command be like? Thanks.
Reply With Quote

#2 Old 2004-10-07, 09:45

Join Date: Oct 2004
Posts: 79
Donz
Offline
Regular Contributor
It is simple:
Code:
Random rnd = new Random();
long pseudoRandomNumber = rnd.nextLong();
Or your question wasn't about class java.util.Random?
Reply With Quote

#3 Old 2004-10-11, 15:31

Join Date: Oct 2004
Posts: 9
Jeff_Ang82
Offline
Registered User
Do you have some examples? I am sort of new to Java. So it would nice for me to look at some examples. Thanks you, anyway.
Reply With Quote

#4 Old 2004-10-11, 15:47

Join Date: Oct 2004
Posts: 79
Donz
Offline
Regular Contributor
I suggess, that you are using Nokia Developer Suite 2.2 (if not, I think you have to install it). Some examples with open source code are included in it. For example, application Boids use class Random.
Here some code from this midlet:
Code:
package example.boids;

import java.util.Random;


class BoidsUtils
{
    private static Random random = new Random();


    private BoidsUtils()
    {
        // private so no-one can create an instance of this class
    }


    static int random(int scale)
    {
        int randomInt = random.nextInt();
        // extend to long, but remove sign-extend
        long randomIntAsLong = (long)randomInt & 0xFFFFFFFFL;
        int result = (int)((randomIntAsLong * (long)scale) >> 32);
        return result;
    }

    // This seems to produce the square root rounded down to the next
    // integer, except for numbers very near the next square, when we
    // get the square root rounded up to the next integer. That's good
    // enough for our purposes here anyway. In use in the boids MIDlet,
    // it averages about 14 iterations per use.
    static int squareRoot(int square)
    {
        int guess = square;
        if (square < 0)
        {
            throw new IllegalArgumentException(
                                         "Negative argument to squareRoot");
        }
        else if (square != 0)
        {
            int oldGuess;
            do
            {
                // Newton's method
                oldGuess = guess;
                guess = (oldGuess + square / oldGuess) >> 1;
            } while ((guess < oldGuess) && (guess > 0));
        }

        return guess;
    }
}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia