1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Random.jar

Discussion in 'Software' started by Edge102030, 26 May 2011.

  1. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    That would be it running out of memory trying to draw characters in the text field

    So as before, you have 2 options
    1 - Increase VM memory usage
    2 - Stop people entering stupid high numbers
    (Secret option 3 - ignore it, assuming no one is stupid enough to enter such a high number)

    2 is my favourite ;)
     
  2. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Two is also my favourite :). I think i understand now, does it see that there is not enough VM memory to copy the output into the textbox and so does not try to? Is that why below it doesn't show as having a full VM?

    [​IMG]
     
  3. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    Yeah basically it asked the Virtual Machine for some more memory (to draw the text), the Virtual Machine couldn't supply enough because it was at its limit, and so threw an Exception.
    This exception caused the thread to end; meaning it didnt get to finish (or start) displaying that text field.

    The two main ways for a thread to end:
    1) Got to the end.
    2) Uncaught exception


    (anyone with better knowledge is free to correct me)
     
  4. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    I can't help you with your error (because I'm too tired to trace it right now >.<) but I do have a suggestion about the overall design:

    Why don't you add a checkbox to include symbols in the password?

    Also, instead of just going with a random case chosen for the letter why not have some bit-twiddling functions that'll make the letter chosen more random (and might be more efficient than the random case choosing mechanism you have setup now depending on how you do it)?
     
  5. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Thanks for explaining that for me Plugs, much appreciated.

    Dae314, on the topic of characters (I think i have an idea for this now), yesterday i posted the random character code as a sub class of the random class, and a guy suggested improving it by using this the below code instead of the switch/case statement.

    Code:
    package random;
    
    import java.util.Random;
    
    public class ExtRandom extends Random {
        
        public String nextLetter() {
    
            return Character.toString((char)(nextInt(26)+97));
        }
    }
    
    I'm thinking i can find the ASCII range of some symbols and do it that way.

    I'm also going to look up what bit twiddling is :).
     
  6. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    1) why not return a character?

    2) if you want to improve the speed of the way you get characters - create an array of all the possible characters you want to use, randomly swap the positions in your constructor, then pick a random number between 0 and that array length

    Code:
    private char[] charArray = {'a', 'b', ... 'A', 'B', ... '!', '$', ... };
    
    //in constructor
    //randomly swap array positions
    for (int i = 0; i < charArray.length; i++){
        int random = r.nextInt(charArray.length);
        char store = charArray[random];
        charArray[random] = charArray[i];
        charArray[i] = store;
    }
    
    
    //get character function
    return charArray[ r.nextInt(charArray.length) ] ;
    
     
  7. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    It's returning strings because of the capitalization in the next part. I'll work on the array way now.

    EDIT:: complete

    The run() function now looks like this:
    Code:
        // thread class
        public class Runner implements Runnable {
                
            public void run() {
            
    
            // prevent more threads from being run
            runButton.setEnabled(false);
            // save time by storing the parsed value into a variable for use later
            // (string length)
            int length = Integer.parseInt(strLength.getText());
    
    
        // string buffer used for concatenation before being sent to the output
        // text box
        StringBuffer outputStr = new StringBuffer(length);    
        
    
        // begin random string creation loop
        for (int idx = 0; idx < length; ++idx){
      
        // call the lettin method (lower/uppercase letters and numbers 1-9)
        outputStr.append(x.Lettin()); 
    
        }
        // set the random string output field to the string created by the thread 
           strOutput.setText(outputStr.toString());         
        
        runButton.setEnabled(true);
        }
       }
    
    And calls on this:

    Code:
    package random;
    
    import java.util.Random;
    
    
    public class Pseud {
        Random r = new Random();
        public char Lettin() {
            
        
        char[] charArray = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
        'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',
        '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1',
        '2', '3', '4', '5', '6', '7', '8', '9'};
    
        //in constructor
        //randomly swap array positions
        //props to Plugs for this 
        for (int i = 0; i < charArray.length; i++){
            int random = r.nextInt(charArray.length);
            char store = charArray[random];
            charArray[random] = charArray[i];
            charArray[i] = store;
    }
            
            
            return charArray[r.nextInt(charArray.length)];
        }
    }
    
    The array has numbers 1-9 twice to increase the likelihood of numbers being in the output.
     
    Last edited: 28 May 2011
  8. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    For symbols (assuming you use the ASCII character codes not the Unicode ones) your hex value range is 21 to 7E. To exclude symbols you have two ranges, numbers: 30 to 39 and letters: 41 to 5A. Note that the letter range is only capitals, but if you want lower case all you need to do is add 0x20 to the capital.

    What I mean by "bit twiddling" is that instead of using a random number generator to grab a random character from an array, you have the program randomize the bits that make up the character. You can probably do something similar to what hash functions do for this.

    Here's a page detailing the bitwise operators in Java.

    If you choose to go this path you'll have to think a bit about how to make sure the character you generate won't be out of range. I personally believe this type of randomization would give you a better (more randomized) password than simply using an a RNG and picking stuff out of an array.
     
    Edge102030 likes this.
  9. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    im not sure if you want to create a new character array every time you call the function, also im not sure you want to randomise every time you call it either

    you literally just want to create a character array, randomise it at the start, and then whenever you want a random character, pick one randomly
    that should be sufficient

    bitwise operations probably will get you better randomness... but then theres tonnes more stuff you could do to get you better randomness than that

    (warning: rambling below)

    if you are worried it isnt random enough, and dont feel confident enough to go into bit operations (or other more technical stuff), you can randomise further by taking only the 3rd nextInt (i.e. you call nextInt() twice and do nothing with it)
    and then with a probability of 1/7 you can make it the 4th rather than the 3rd

    these things stop you using the exact output of a pseudo random generator, making it slightly more random.. although still pseudo random
     
    Edge102030 likes this.
  10. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    I'm not (overly) worried about how random it is since i'm not using it for anything that needs to be super secure. I think it is interesting to experiment with the options to learn more and see what works.

    @Dae314, the demo on the oracle site is combining two hex values and printing the result as a string by ASCII indexing right? Or have i got something confused here.

    I'd like to thank you guys for your input and suggestions (it is much appreciated!! +rep to you both).
     
  11. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    i believe the demo is doing a bitwise AND operation, and printing the Hex output (because the input is Hex)

    to differentiate Hex we put 0x at the front (0xF = 15 decimal)
    each Hex character is equivalent to 4 bits (0x0 = 0000 , 0xF = 1111 )

    bitwise AND on single bits: returns 1 if both inputs are 1
    1 AND 1 = 1,
    0 AND 1 = 0, 1 AND 0 = 0, 0 AND 0 = 0

    with multiple bits we do the same as single bits, just more times
    111 AND 100 = 100
    101 AND 111 = 101

    so the demo took the Hex, converted it into binary (figuratively, as it was likely stored as binary in the first place), and performed the AND operation, and return the bitstring as a Hex
    Code:
    0x000F = 0000000000001111  AND
    0x2222 = 0010001000100010
           = 0000000000000010 = 0x0002
    
    *Edit: the output is equally likely to be decimal if it is just "2" - it is an integer after all
     
  12. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    It's a simple bitwise AND operation that goes exactly as plugs said. Because the program doesn't use printf() and val and bitmask are integers it'll print the result out as a decimal.

    Bit masking might be helpful to you since you need to restrict the range of values. For instance if you only want the first (reading binary goes right to left) 8 bits after you've randomized the bit pattern you can apply a mask like 0x00FF (0000 0000 1111 1111) to your value and it'll keep only the first 8 bits. This works because p & True == p (True being 1 in this case) while p & False == False (False being 0). Also remember & is reflexive so p & q == q & p. p and q in these examples are variables representing boolean values (either a 1 or a 0).

    Bitwise randomization is not necessary, but I believe it'd give you a greater degree of password randomness and thus a greater degree of strength. It sounds like you're not too familiar with bit twiddling so maybe you would want to play with it a little here just to learn it :). Doing little bit twiddling hacks can sometimes improve the performance of your code (they're not necessary for good code though). Bit twiddling means that you probably have to document your code well though because it's harder to see exactly what's happening (example: bit twiddling to detect opposite signs). Maybe as an extra you can add a checkbox that'll say "Enable bitwise randomization" or something like that :).
     

Share This Page