Random Number Generator acting weird

Discussion in 'Plugin Development' started by PatrickFreed, Jul 16, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    PatrickFreed

    Hi all-

    I'm very new to java/coding and I really don't know much. Most of what I code is just from looking at other plugins' sources and duplicating to fit my scenario. Anyways, to the problem.

    Source (real one this time)
    Plugin (for testing)

    Be wary, the code is messy (not my fault, the original dev's fault ;p)

    For the plugin, I want it to have three modes: Percentage, Static, and random. I have percentage ans tatic working, but Random is a bit finicky.

    It generates extremely strange numbers and percentages, ranging from -4k to 1k+ :p

    So I was just wondering if you more experienced people took a look at it and would tell me what's going wrong. Also, if you guys could tell me why it generates a blank config.yml that'd be great. (Also, give me some hinters about parts that could be rewritten better)

    Thanks, -Pat

    (Also, if this is unclear, it's because I'm tired and my writing goes to shit when I'm tired ;p)
     
  2. Offline

    DreadKyller

    I don't even see anything about random in the entire 3 java files. Which file is it in and maybe I can try to see what's wrong.
     
  3. Offline

    PatrickFreed

    Shit i uploaded wrong source, one sec
    Edit: re-download :)
     
  4. Offline

    beatcomet

    Here is the shortest generator I found (actually I made it myself while tried to replace a 150 lines of word generator)
    Here is how it's working :

    Code:java
    1.  
    2. //Define the length of the word (in your case - number), you can set at to any number you want
    3. int length = 8;
    4.  
    5. //This String contains all the numbers (you can also add letters to it, to generate a random mix of letters and numbers)
    6. String numbers : "123456789";
    7.  
    8. //define the rasult
    9. String myresult = "";
    10.  
    11. //random generator
    12. Random rand = new Random();
    13.  
    14. //use while loop to check if the length of the current number equlas to the lehngth defined above
    15. while (myresult.length() < length){
    16.  
    17. //generate a random number, the number will be used to get one of the numbers from the String numbers later on
    18. int x = rand.nextInt(numbers.length());
    19.  
    20. //add one of the numbers to the current result
    21. myresult = myresult + numbers.charAt(x);
    22. }
    23.  
    24. //The end, you can add here a print command to see if it works
    25. System.out.println(myresult);
    26.  


    enjoy :)
     
  5. Offline

    PatrickFreed

    Thanks, but I got it working. I had a +1 in the wrong place on the generator. :)
     
  6. Offline

    DreadKyller

    nice that you got it working.
     
Thread Status:
Not open for further replies.

Share This Page