Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android random string generator

Tags:

android

I have a problem. I want to draw a random String something like this aXcFg3s2. What i doing bad ? How change my random()

private String random;
private String charsEntered;
private EditText et;
private Button ok;
CaptchaInterface.OnCorrectListener mCorrectListener;

public void setOnCorrectListener(CaptchaInterface.OnCorrectListener listener) {
    mCorrectListener = listener;
}

public TextCaptcha(Context context) {
    super(context);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}

public static String random() {
    Random generator = new Random();
    String x = (String) (generator.nextInt(96) + 32);
    return x;
}

public void onCreate(Bundle icicle) {
    setContentView(R.layout.main);
    random = random();
    TextView display = (TextView) findViewById(R.id.textView1);
    display.setText("Random Number: " + random); // Show the random number
    et = (EditText) findViewById(R.id.etNumbers);
    ok = (Button) findViewById(R.id.button1);
    ok.setOnClickListener(this);

}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    try {
        charsEntered = et.getText().toString();
    } catch (NumberFormatException nfe) {
        Toast.makeText(et.getContext(), "Bla bla bla",
                Toast.LENGTH_LONG).show();
    }

    if (random == charsEntered) {
        Toast.makeText(et.getContext(), "Good!", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(et.getContext(), "Bad!", Toast.LENGTH_LONG).show();
    }
}
like image 759
Defus Avatar asked Aug 24 '12 20:08

Defus


People also ask

How do I generate random characters on Android?

you can create an array of characters which has all of the characters that you wish to allow to be in the random string , then in a loop take a random position from the array and add append it to a stringBuilder . in the end , convert the stringBuilder to a string. Show activity on this post.

How do you generate random strings?

Using the random index number, we have generated the random character from the string alphabet. We then used the StringBuilder class to append all the characters together. If we want to change the random string into lower case, we can use the toLowerCase() method of the String .

How to generate random number in a given range on Android?

This example demonstrates how do I generate random number in a given range on android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to generate a random string in Java?

Try: Random generator = new Random(); String x = String.valueOf (generator.nextInt(96) + 32); Share Improve this answer Follow

How to generate secure random Alpha and numeric string in Java?

Make call to generateAlphaNumericString () function. It should work. below vote label (+1) you will get right icon, select that. Here is good example of generating secure random alpha and numeric string using SecureRandom import java.security.SecureRandom; import java.math.BigInteger;

How to add random characters to a string?

you can create an array of characters which has all of the characters that you wish to allow to be in the random string , then in a loop take a random position from the array and add append it to a stringBuilder . in the end , convert the stringBuilder to a string. EDIT: here's the simple algorithm i've suggested:


3 Answers

the problem is that you've handled only a single character instead of using a loop.

you can create an array of characters which has all of the characters that you wish to allow to be in the random string , then in a loop take a random position from the array and add append it to a stringBuilder . in the end , convert the stringBuilder to a string.


EDIT: here's the simple algorithm i've suggested:

private static final String ALLOWED_CHARACTERS ="0123456789qwertyuiopasdfghjklzxcvbnm";  private static String getRandomString(final int sizeOfRandomString)   {   final Random random=new Random();   final StringBuilder sb=new StringBuilder(sizeOfRandomString);   for(int i=0;i<sizeOfRandomString;++i)     sb.append(ALLOWED_CHARACTERS.charAt(random.nextInt(ALLOWED_CHARACTERS.length())));   return sb.toString();   } 

and on Kotlin:

companion object {     private val ALLOWED_CHARACTERS = "0123456789qwertyuiopasdfghjklzxcvbnm" }  private fun getRandomString(sizeOfRandomString: Int): String {     val random = Random()     val sb = StringBuilder(sizeOfRandomString)     for (i in 0 until sizeOfRandomString)         sb.append(ALLOWED_CHARACTERS[random.nextInt(ALLOWED_CHARACTERS.length)])     return sb.toString() } 
like image 87
android developer Avatar answered Oct 13 '22 21:10

android developer


There are a few things wrong with your code.

You cannot cast from an int to a string. Cast it to a char instead. This however will only give you a single char so instead you could generate a random number for the length of your string. Then run a for loop to generate random chars. You can define a StringBuilder as well and add the chars to that, then get your random string using the toString() method

example:

public static String random() {     Random generator = new Random();     StringBuilder randomStringBuilder = new StringBuilder();     int randomLength = generator.nextInt(MAX_LENGTH);     char tempChar;     for (int i = 0; i < randomLength; i++){         tempChar = (char) (generator.nextInt(96) + 32);         randomStringBuilder.append(tempChar);     }     return randomStringBuilder.toString(); } 

Also, you should use random.compareTo() rather than ==

like image 26
bennettaur Avatar answered Oct 13 '22 23:10

bennettaur


You need to import UUID. Here is the code

import java.util.UUID;

id = UUID.randomUUID().toString();
like image 22
शु-Bham Avatar answered Oct 13 '22 21:10

शु-Bham