Thursday, April 1, 2010

Generate random number in Android

To generate random number in Android, class java.util.Random can be used.

This class java.util.Random provides methods that generates pseudo-random numbers of different types, such as int, long, double, and float.

It support two public constructor:
Random() - Construct a random generator with the current time of day in milliseconds as the initial state.
Random(long seed) - Construct a random generator with the given seed as the initial state.

Generate random number in Android

main.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random number"
android:id="@+id/generate"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/generatenumber"
/>
</LinearLayout>


AndroidRandom.java
package com.exercise.AndroidRandom;

import java.util.Random;

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

public class AndroidRandom extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Random myRandom = new Random();

Button buttonGenerate = (Button)findViewById(R.id.generate);
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);

buttonGenerate.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
}});
}
}


Related:
- Generate sequence of Random number.



9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. quick question what If i want to generate a random number of 12 characters and create a database and every random number generated is saved into that database?

    ReplyDelete
  3. hello chilltime,

    To generate sequence of random number, please refer Generate Random number, Random().

    Gore database, pls refer A simple example using Android's SQLite database

    But if you only want to save 12 random number, I think SharedPreferences.Editor is much more easy.

    Hope can help.

    ReplyDelete
  4. The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})

    why won't this pass?

    ReplyDelete
  5. hello MooseAMoose,

    DialogInterface!? are u mis-typing? or import a wrong class?

    ReplyDelete
  6. I copied and pasted this right into eclipse. ctrl+shift+o and then tried to run.

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

    ReplyDelete
  8. how to generate 20 random question and its 4 options out of 100 given questions with each 4 options from sqlite database

    ReplyDelete
  9. How can I generate random numbers without going back the same number? For example, I have 20 numbers. When "15" and "6" is already picked out, it will not be printed again to the next process output. Help please, thank you

    ReplyDelete