Questions About Randoms In Java...

Discussion in 'Gaming' started by White B O I, Nov 17, 2008.

  1. White B O I

    White B O I Well-Known Member

    Age:
    36
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    I know hardly anything about Java, but there is this script, that I cannot release, that I need to add a random to for a friend.

    I offered to do it so I could learn :D
    Here is what I need (in psuedo code)

    Some Number between 1 and 10 Randomized
    case 0: number = 1 so "This text appears"
    case 1: number = 2 so "This text will appear"

    So basically a number between 1 and 10 (including 1 and 10) are randomized and assigned a case number (0-9)
    And if that number is randomly chosen, it will choose that case number and display the assigned text.

    I can clarify more, possibly, I'm not to sure, its hard to lay ideas from my mind into words =]
     
  2. Prince Zainx

    Prince Zainx The Dark Prince

    Posts:
    10,918
    Likes Received:
    0
    Joined:
    Jun 13, 2005
    Location:
    A world far beyond your imagination...
    Assuming that you really are White and Ben Affleck you should know this stuff...

    Anyhow this works fine, wrote it a min ago and tested it...

    Code:
    EDIT: Commented it for you if that was required.
    Code:
    import java.util.*;
    
    public class Rand {
    
        /** Method to Return a Random integer Data Value.
         @param Value to Generate Random Integers to.
         @return The Random Data Value.
        */
        public static int getRandom(int n) {
            Random gen = new Random();
            return gen.nextInt(n) + 1;
        }
    
        public static void main(String[] agr){
            // An integer named number.
            int number;
    
            /*
            * Generates a Random Number from 1 to the number stated
            * and stores it in the integer number.
            */
            number = getRandom(10);
    
            /* Prints out what the Random Number is to make sure
            * the Case listed below matches the number. Can be changed
            * to any statement of chosing.
            */
            System.out.println("The Random Number is: " + number);
    
            /*
            * Takes the Number and all of the cases of the number
            * in this Case it is from 1 to 10 and each case
            * displays a certain message. It can be used to display
            * as many Cases as you desire.
            */
            switch (number) {
                case 1:  System.out.println("Case if Number = 1");
                break;
                case 2:  System.out.println("Case if Number = 2");
                break;
                case 3:  System.out.println("Case if Number = 3");
                break;
                case 4:  System.out.println("Case if Number = 4");
                break;
                case 5:  System.out.println("Case if Number = 5");
                break;
                case 6:  System.out.println("Case if Number = 6");
                break;
                case 7:  System.out.println("Case if Number = 7");
                break;
                case 8:  System.out.println("Case if Number = 8");
                break;
                case 9:  System.out.println("Case if Number = 9");
                break;
                case 10: System.out.println("Case if Number = 10");
                break;
    
            }
    
        }
    }
    That will be 1,000 Credits Sir ;)

    Enjoy.

    Zain.
     
  3. White B O I

    White B O I Well-Known Member

    Age:
    36
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    Why thank you =] i doesnt got 1000 creds, but I will rep you =]
     
  4. Prince Zainx

    Prince Zainx The Dark Prince

    Posts:
    10,918
    Likes Received:
    0
    Joined:
    Jun 13, 2005
    Location:
    A world far beyond your imagination...
    Damn ... was hoping for the 1,000 Credits <_<

    :D

    Zain.
     

Share This Page