Java Programming Tutorial – 26 – Random Number Generator

How to use a random number generator in java programming.


Comments

25 Responses to “Java Programming Tutorial – 26 – Random Number Generator”

  1. magic666z on November 22nd, 2009 4:35 pm

    can you do something like… for my FRP (DND) game i want to put like health from 10 to 100 Dmg from 5 to 20 Armor 1 to 15 and and random it up?

  2. pempek8282 on November 29th, 2009 4:59 pm

    System.out.println(“Thanx”);

  3. IncludeAustin on December 1st, 2009 7:47 pm

    Of course, just take the difference and add the minimum #

  4. magic666z on December 1st, 2009 7:51 pm

    ok I will try it…
    thank you :)

  5. Tubbergen on December 5th, 2009 1:14 pm

    I prefer to use Math.random() so I dont have to import anything.

    explanation:
    Math.random() outputs a decimal number between 0 and 1 (0.1, 0.5, 0,7) I multiply that number with 6 = (0.6*6 = 3.6 , 0.2 * 6 = 1.2 etc.) then I add 1 and cast that into an int: So the whole thing will be: (int)(Math.random() * 6 + 1);

    What do you people think of this method? is it long-winded or..?

  6. Tubbergen on December 5th, 2009 1:18 pm

    Saucebiotch, I know it’s been 3 weeks now since you posted this comment but for the 2nd problem is this a possibility: If(inputString.equalsIgnoreCase(“quit”) {
    //break loop..
    }else {
    //proceed
    }

  7. LawenceCodye on December 21st, 2009 3:54 pm

    what pempek8282 said…enough said…

  8. Firebreath01 on December 22nd, 2009 11:24 pm

    That’s still good but it’s not pseudo-random.

  9. hmk25061984 on January 17th, 2010 1:59 am

    for more information abou random i recommend u to seee

    java(dot)sun(dot)com(slash)j2se(slash)1.4.2(slash)docs(slash)api(slash)java(slash)util(slash)Random(dot)html

    and for any query about any thing in java i welcome u all to add me at my skypeid/google talk id=hmk25061984

  10. hmk25061984 on January 17th, 2010 2:12 am

    like random generator i have random behaviour , is there is any normal generator ..)

  11. Hellfire928 on January 19th, 2010 8:40 pm

    what compiler is he using?

  12. farmorcn on January 31st, 2010 1:58 pm

    @Hellfire928 Look at the earliest tutorials, i think around 1-5, there he tells what he uses and so on..

  13. Carlthebiff on February 5th, 2010 3:25 am

    @SirStiller
    Addition is commutative so it wouldn’t really make a difference

    @ultimatedorkwad
    This would work but surely it’s easier to just add the 1 rather than typing a whole if statement?

    @Hellfire928
    The compiler built in the Eclipse IDE :)

    Good vid, Bucky! I only knew of the Math.random way until now.

  14. sharkristella on March 6th, 2010 12:01 pm

    Thanks so much

  15. malabgc18 on March 7th, 2010 6:55 am

    bucky is using eclipse

  16. Hauserdav on March 13th, 2010 2:18 pm

    If i do this prigramm with a while loop it puts only 1 number out?!
    Can anyone tell me y?

  17. Hauserdav on March 13th, 2010 2:20 pm

    we dont want bucky to die!!!

  18. nthenameof on March 20th, 2010 10:48 pm

    Good tutorial, but say you wanted to take that random number and add or divide it to another random number and print the result?? for example, 1 + 3 = 4, or 4 / 2 = 2.. How would you do it ? And to add, how would you place those random number if need be in a range, e.g. numbers random numbers between 1 and 10, or random numbers between 10 and 20 ?

  19. pwnions08 on March 26th, 2010 12:18 am

    @Hauserdav
    Need to have your condition set in there

    while(condition/arg)

    for example while(true)
    while(variable>=10)
    just play with it :P

  20. Kulexy on April 3rd, 2010 4:39 pm

    You Are amazing… Thanks a Lot!!

  21. 4sh4l33 on April 8th, 2010 10:18 am

    @nthenameof HEy, i was wondering that too… did u end up with a solution??/

  22. 2plus2equalswhat on April 10th, 2010 12:04 pm

    random tip for all viewers: if you don’t want to keep typing “System.out.println(” “);”

    just do:

    (The numbers next to the brackets are to show relate them)

    class example{1
    public static void main(String args[]){2
    }2
    private static void log(String aMessage){3
    System.out.println(aMessage);
    }3
    }1
    just type that within your class brackets and outside of your public static brackets

    now instead of typing System.out.println(” “); you type: log(” “);

  23. bladeofend on April 13th, 2010 8:14 am

    tnongs

  24. realdenni199 on April 18th, 2010 3:12 pm

    @2plus2equalswhat or you cud write syso and then hold CTRL click space and release both

  25. kruszczu on April 18th, 2010 4:57 pm

    Radom is actually a city in Poland :) made me laugh

Powered by Yahoo! Answers