GENERATING RANDOM NUMBERS IN JAVA

Hi Fellows, in my previous post (GENERATING UNIQUE RANDOM STRING AND ITS APPLICATION) , I blogged about generating random numbers in VB.net.

Below is its version in JAVA. Enjoy !!!

private String generateRandom(Integer key) {

StringBuffer randomString = new StringBuffer();

String letters = “abcdefghijklmnopqrstuvwxyz“;

String numbers = “0123456789“;

char[] numbersArray = numbers.toCharArray();

char[] lettersArray = letters.toCharArray();

Integer arrIndex = -1;

 

for (int i = 0; i <= key; i++) {

//generate any random number from 1 -100, if them number is even,

//pick a letter randomly else pick a number

int rnd = new Random().nextInt(100);

if (rnd % 2 == 0) {

arrIndex = new Random().nextInt(24);

randomString.append(lettersArray[arrIndex]);

} else {

arrIndex = new Random().nextInt(10);

randomString.append(numbersArray[arrIndex]);

}

}

return randomString.toString();

}

Below is a sample generated strings when application is run.

GENERATING UNIQUE RANDOM STRING AND ITS APPLICATION

Generating random numbers can be sometimes frustrating, especially when you want them to be unique.

Example in this instance is when you give users unique usernames when they register with you websites.

Below is a simple function to generate random numbers. To make these generated random strings unique (for the purpose of your application you can add the row index, when inserting it into a database, to make it unique. In this case you will be generating always unique random variables)

Function Generate() As String
‘key_chars, is the length of random numbers u want to generate

Dim i_key As Integer

Dim RandomNo As Single
Dim arrIndex As Int16

Dim sb As New StringBuilder

Dim RandomLetter As String

lstLetters = "abcdefghijklmnopqrstuvwxyz"

lstNumber = "0123456789"

LettersArray = Me.Key_Letters.ToCharArray

NumbersArray = Me.lstNumber.ToCharArray


For i_key = 1 To Key_Chars

Randomize()

RandomNo = Rnd()

arrIndex = -1 ‘the number 101 was randomly chosen

If (CType(Random1 * 101, Integer)) Mod 2 = 0 Then

‘if the number generated from the if condition is even get a letter else get a number

‘ generate a random number from the letters array


Do While arrIndex < 0

arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * RandomNo)

Loop

RandomLetter = LettersArray(arrIndex)

sb.Append(RandomLetter)

Else

Do
While arrIndex < 0

arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * RandomNo)

Loop

RandomLetter = NumbersArray(arrIndex)

sb.Append(RandomLetter)

EndIf

Next

Return sb.ToString

End Function

How to Encrypt Strings in .NET –The easiest way

Whatever application you develop been it for the national security or for a one-man provision kiosk, some data should be encrypted. Things like database configuration variables, user login passwords and some application settings. In this article we will go through two methods of encrypting your variable with a basic knowledge in computing. The first been saving your records in bytes and the other in scrambled strings

Saving as bytes

Public Shared Function encrypt(ByVal password As String) As Byte()

Dim byteValue() As Byte = Encoding.UTF8.GetBytes(password)

Return byteValue

End Function

 

Public Shared Function decrypt(ByVal pass As Byte()) As String 

Dim password As String = Encoding.UTF8.GetString(pass)

Return password

End Function

 

For example, to encrypt your password fields, in the database design make the data type of the field and image. Then in your code write the simple methods below. You are done. At least you have encrypted the field to some extent; no one can easily get the password by easily checking from the backed

Encrypting Strings

In cases where you want to save your password as strings, or saving some application settings in the “My Settings” one simple way is by encrypting the string by adding a chosen value to each character in the string to change its meaning. Check the methods below

    Private Const code As Integer = 5

    Private Function StrEncrypt(ByVal str As String) As String

        Dim scrambled As String = “”

        Dim letters() As Char = str.ToCharArray

        For i As Integer = 0 To letters.Length – 1

            scrambled = scrambled + ChrW(AscW(letters(i)) + code)

        Next

        Return scrambled

    End Function

 

    Private Function StrDencrypt(ByVal str As String) As String

        Dim unscrambled As String = “”

        Dim letters() As Char = str.ToCharArray

        For i As Integer = 0 To letters.Length – 1

            unscrambled = unscrambled + ChrW(AscW(letters(i)) – code)

        Next

        Return unscrambled

    End Function

Check the picture below ,when the runnibg the application

encrpt