Saturday, October 29, 2011

Generate Random number, Random()

java.util.Random is a class provides methods that return pseudo-random values.

Normally (in other language/system), we will generate random number with seek of current time, to try to make the pseudo-random un-predictable. But in case of Android, it's not necessary and not recommended - Because "It is dangerous to seed Random with the current time because that value is more predictable to an attacker than the default seed." - refer to Android document java.util.Random.

The default constructs, Random(), already come with an initial state that is unlikely to be duplicated by a subsequent instantiation.

Here is a example to generate 10 integer using Random().

Generate Random number, Random()

package com.exercise.AndroidRandom;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidRandomActivity extends Activity {

Button generateRandom;
TextView randomResult;
Random myRandom;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generateRandom = (Button)findViewById(R.id.generate);
randomResult = (TextView)findViewById(R.id.randomresult);

generateRandom.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String result = "";
myRandom = new Random();

for(int i = 0; i < 10; i++){
result += String.valueOf(myRandom.nextInt()) + "\n";
}

randomResult.setText(result);
}});

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/generate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random Number"
/>
<TextView
android:id="@+id/randomresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

5 comments:

  1. Can you help me determine why this forceclose?

    01-03 21:44:20.685: E/AndroidRuntime(363): FATAL EXCEPTION: main
    01-03 21:44:20.685: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.exercise.AndroidRandom/com.exercise.AndroidRandom.AaActivity}: java.lang.ClassNotFoundException: com.exercise.AndroidRandom.AaActivity in loader dalvik.system.PathClassLoader[/data/app/com.exercise.AndroidRandom-2.apk]
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-03 21:44:20.685: E/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
    01-03 21:44:20.685: E/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:521)
    01-03 21:44:20.685: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    01-03 21:44:20.685: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    01-03 21:44:20.685: E/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
    01-03 21:44:20.685: E/AndroidRuntime(363): Caused by: java.lang.ClassNotFoundException: com.exercise.AndroidRandom.AaActivity in loader dalvik.system.PathClassLoader[/data/app/com.exercise.AndroidRandom-2.apk]
    01-03 21:44:20.685: E/AndroidRuntime(363): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    01-03 21:44:20.685: E/AndroidRuntime(363): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    01-03 21:44:20.685: E/AndroidRuntime(363): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    01-03 21:44:20.685: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    01-03 21:44:20.685: E/AndroidRuntime(363): ... 11 more

    ReplyDelete
  2. try

    randomResult.setText("" + result + "");

    i'm a novice programmer but i've had issues with just putting strings in setText methods to set a TextView, I just add the quotations on both sides cause i don't know if it add space or not that way if so its still centered.

    ReplyDelete
  3. try

    randomResult.setText("" + result + "");

    i'm a novice programmer but i've had issues with just putting strings in setText methods to set a TextView, I just add the quotations on both sides cause i don't know if it add space or not that way if so its still centered.

    ReplyDelete
  4. hello Peter Gil Sulima,

    Do you means generate random String?

    There are many way to generate random strings, one of them is using org.apache.commons.lang3.RandomStringUtils.

    ReplyDelete