Java Random Function

1. ## Java Random Function

Hello guys,
I want to know how i can generate a random value in java, and which package will be included,
for example i want to add 4 random values

int i=0;
i+=function();

and second one how i can convert a string into capital latters in java.

PUNJABIAN263

2. Creating random numbers

The Math.random() method returns a floating-point value between 0 and 1. If you
design a script to act like a card game, you need random integers between 1 and 52;
for dice, the range is 1 to 6 per die. To generate a random integer between zero
and any top value, use the following formula:

Code:
`Math.floor(Math.random() * n)`
Here, n is the top number. To generate random numbers between a different range,
use this formula:

Code:
`Math.floor(Math.random() * n) + m`
Here, m is the lowest possible integer value of the range an n equals the top number
of the range. For the dice game, the formula for each die is:

Code:
`newDieValue = Math.floor(Math.random() * 6) + 1`

3. This should turn lower case to upper case, I only tested it on a couple of things though.

Code:
```String s= "lslsliuyt";

for(int i=0; i<s.length(); i++)

{

if((int)s.charAt(i)>90)

{

int x = (int)s.charAt(i)-32;

System.out.print((char)x);

}

else

System.out.print(s.charAt(i));

}```

4. Look at the java 2 api, and look for String ( http://java.sun.com/j2se/1.4.2/docs/api/ ). There's a toUpperCase() method.

5. I think there is an error in the code above...i am just starting to learn Java so i may be wrong but this is what the code above should look like:
Code:
```String s= "lslsliuyt";

for(int i=0; i<s.length; i++)

{

if((int)s.charAt(i)>90)
{

int x = (int)s.charAt(i)-32;

System.out.print((char)x);

}

else

System.out.print(s.charAt(i));

}```
Anyways, when you are takling about strings, there is no brakets at the end of length....

also to make a random number generator, this is what the code should look like:
Code:
```import java.util.Random;

public class Arrays  //you can change the name Arrays to whatever your program is called
{
public static void main (String args[])
{
Random r = new Random();
int myVar=0;

for (int i=0;i<100;i++)
{
myVar=r.nextInt();//gets random *******s
System.out.println("A random number is: " +myVar);//prints this 99 times
}
}
}```
Once again i dont know how this program will run...but it should look something like that hope i helped

6. I think you have to use the parenthesis with the length() because it is a method. Even if you don't have to, it still works. But the best solution would probably be to just use the built in toUpperCase() method like Guus said.

#### Posting Permissions

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