Software Archive
Read-only legacy content
17060 Discussions

Android random function

JLuna5
New Contributor I
27,869 Views

Hi,

I'm learning  to developer on android, and I'm making a exercise of Sharedpreferences, but I don't know how to generate a random number and show into textview on my app.

0 Kudos
14 Replies
JLuna5
New Contributor I
27,869 Views

Estoy aprendiendo a desarrolladores en Android, y estoy haciendo un ejercicio de Sharedpreferences, pero no sé cómo generar un número aleatorio y mostrar en TextView en mi aplicación.

0 Kudos
Bernard
Valued Contributor I
27,869 Views

Hi JudLup

Sadly I do not know Android platform,but Android is written in Java and probably has built in math library.I would also recommend you to check posibility of importing Java classes.

0 Kudos
SergeyKostrov
Valued Contributor II
27,869 Views
>>... I don't know how to generate a random number... Here is example: ... class TestSet { ... TestSet() { float r = ( ( float ) Math.random() - 0.5f ) * 0.2f; ... } ... }
0 Kudos
Mohamed_helmi_b_
Beginner
27,869 Views

you can use "math.random". this methode generate a number between 0 and 1. you can multlply it with the max number that you want to reach.

0 Kudos
Mohamed_Ali_A_
Beginner
27,869 Views

Here are some examples with results : 

Math.random()

Math.round(Math.random()*10); // 1 digit random

Math.round(Math.random()*100); //2 digit random

Math.round(Math.random()*1000); //3 digit random

Results:

Math random value :0.8393086905318781

Math random value :3

Math random value :82

Math random value :719

0 Kudos
Ahmed_BH
Beginner
27,869 Views

Hi JudLup Luna,

try this example code it can help you


<?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>


RandomNumber.java
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 RandomNumber 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()));
                                  }
                      });
       }
}

0 Kudos
JLuna5
New Contributor I
27,869 Views

Hi iliyapolak, thank you for you suggestion.

0 Kudos
JLuna5
New Contributor I
27,869 Views

Hi Sergey Kostrov, Thank you for you suggestion.

0 Kudos
JLuna5
New Contributor I
27,869 Views

Sergey Kostrov thank you!

0 Kudos
JLuna5
New Contributor I
27,869 Views

Mohamed Ali A. thank you!

0 Kudos
JLuna5
New Contributor I
27,869 Views

HI Ahmed BH , thank you for you answer, this is very complete.

0 Kudos
Ashwin_S_Ashok
Beginner
27,869 Views

there are random function class in java..

random r= new random();

 

0 Kudos
Ashwin_S_Ashok
Beginner
27,869 Views

Random random = new Random();

int ran=random.nextInt(100);

here is the code..

0 Kudos
Raghavendr_U_Intel
27,869 Views

to an extent you will have to bank on Java capabilities. I saw some examples which you can use for Random number generation. Other answers here in this thread is also very valid.

http://www.javapractices.com/topic/TopicAction.do?Id=62

0 Kudos
Reply