| Reply | « Previous Thread | Next Thread » |
|
Does anyone know how to use random function? What will the command be like? Thanks.
|
| Jeff_Ang82 |
| View Public Profile |
| Find all posts by Jeff_Ang82 |
|
It is simple:
Code:
Random rnd = new Random(); long pseudoRandomNumber = rnd.nextLong(); |
|
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.
|
| Jeff_Ang82 |
| View Public Profile |
| Find all posts by Jeff_Ang82 |
|
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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|