Java Random Function
Results 1 to 6 of 6

Thread: Java Random Function

  1. #1
    Banned
    Join Date
    Feb 2003
    Posts
    35

    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.
    thanks in advance.

    PUNJABIAN263

  2. #2
    Member
    Join Date
    Feb 2002
    Posts
    84
    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
    I hope this will help you a little further.
    [shadow]OpenGL rules the game[/shadow]http://www.AntiOnline.com/sig.php?imageid=499

  3. #3
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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. #4
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    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.
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  5. #5
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #6
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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
  •