Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive filter_list
Author
Message
[JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #1
If you guys have seen my recent thread, I have started learning Java Programming and thus far, am loving it!

This is my first Java Snippet release and what this is, is a Random Number Generator.

NOTE: THIS IS ONLY A SOURCE, SO YOU MUST HAVE A COMPILER OF YOUR OWN TO RUN/USE

The file MUST BE NAME "RandomNumGen.java" for it to compile
Code:
//Brought to you by Jacob - Hackcommunity.com - The Best Ethical Hacking Forums
//                           Come pay us a visit!
//Bare with me, i've just started learning Java Programming :)
//This is my first Java Snippet Release :D

import java.util.Random;

class RandomNumGen{
    public static void main(String[] args){
            Random random = new Random();
            int max = 4; //This is where you will replace the 0 with the MAXIMUM number you would like to generate
            int amount = 10; //This is where you will replace the 10 with the AMOUNT of numbers you would like to generate
            int out;
            
            for(int counter = 1; counter <= amount;counter++){
                out = 1+random.nextInt(max);
                System.out.println(out + " ");
            }
        }
    }

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #2
cool snippet but i think this is like VC++ Biggrin
Pierce the life fibers with your drill.

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #3
(02-10-2012, 11:40 AM)1234hotmaster Wrote: cool snippet but i think this is like VC++ Biggrin

Java has C-Syntax.

This is definitely java "import java.until.x" says so xD

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #4
Java has syntax similar to C++ (which is a superset of C, basically C extended with OOP programming paradigm and some minor differences), because it's derived from this language, plus designers of this language wanted to make it as easy to learn for existing C/C++ programmers at the time.

Regarding the snippet, there's not really much to say, it's relatively primitive and useless code (no offense, it's just usage of the Java library without really adding anything useful on its own).

However, there are a few things I might point out.

Code:
for(int counter = 1; counter <= amount;counter++)
While technically correct, it's a convention to count from zero and do this type of for loop like this:
Code:
for(int counter = 0; counter < amount;counter++)
Remember, in programming, counting usually starts from 0, instead of 1.

Additionally, you don't even need the out variable, you can do following:
Code:
out = 1+random.nextInt(max);
System.out.println(out + " ");
More simply like this:

Code:
System.out.println( (1+random.nextInt(max)) + " ");

What I would suggest would be implement this like a function, that will take the max and amount as input parameters, so it can be used in various places in the code. Instead of printing them right on screen, it can create an array of random numbers with the given amount and return that. You can always print this array on the screen or reuse it on other parts of your program.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #5
woah slow down...i just started java 3 days ago..

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #6
So that means that I can't give you any advices and pointers?
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #7
(02-10-2012, 07:08 PM)Frooxius Wrote: So that means that I can't give you any advices and pointers?

it was a lot to take in at once, but i definitely loved the constructive criticism Smile

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #8
I think you are exaggerating a lot, what I pointed out were really simple matters, you don't need to be a genius to understand them.

The first one simply starts counting up from zero instead from one and changes condition smaller than or equal to smaller than, so the amount of iterations remains the same, I don't see what's complex about that.

Second example simply takes the expression that you assign to the variable named out and places it at the place, where was the variable out originally used when printing the result on the screen.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: [JAVA][SNIPPET]Random Number Generation - HackCommunity Exclusive #9
Code:
import java.util.*;

public class PasswordGenerator
{
    private static Random noise = new Random();
    
    
    public static char randomLetter()
    {
      int x;
      x = noise.nextInt(25)+ 97;
      char c = (char) x;
      return c;
    }
    
   public static StringBuilder makePassword(int y)
   {
       StringBuilder passBuilder = new StringBuilder(32);
       while (y > 0)
       {
           passBuilder.append(randomLetter());
           y--;
       }
       return passBuilder;
   }
  
   public static void printPassword()
   {
        int i = 1;
        do
        {
             System.out.println(makePassword(8));
             i++;
        }
        while( i <= 10);
            

        
   }
  
public static StringBuilder makeBetterPassword(int y)
   {
       StringBuilder password = new StringBuilder(32);
      for (int x = y -1; x > 0; x--)
      {
           password.append(randomLetter());
      }
      password.append(noise.nextInt(7)+ 2);
      if (noise.nextBoolean() == true)
      {
          password.reverse();
      }
        return password;
   }
    public static void makeEvenBetterPassword(int y)
   {
       StringBuilder betterPassword = new StringBuilder(makeBetterPassword(y));

       char uScore = '_';
       for (int i = y - 1; i > 0; i--)
       {
          
           if(betterPassword.charAt(i) == 'l')
           {
               betterPassword.setCharAt(i, uScore);
           }  
           if(betterPassword.charAt(i) == 'o')
           {
               betterPassword.setCharAt(i, uScore);
           }        
          
       }
       System.out.println(betterPassword);
    }
    
}

Just pulled this out of a class file a did a while back. It's based on the same principles as your post, also before anyone gets a chance; I know the passwords are weak - it's more of a demo.

Sorry for a possible thread hijack, just didn't think it was worth it's own post and it relates to OP Smile

AP1

Reply







Users browsing this thread: 2 Guest(s)