Android random function

Android random function

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.

JudLup Luna
15 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

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.

JudLup Luna

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.

>>... 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;
...
}
...
}

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.

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

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

Hi iliyapolak, thank you for you suggestion.

JudLup Luna

Hi Sergey Kostrov, Thank you for you suggestion.

JudLup Luna

Sergey Kostrov thank you!

JudLup Luna

Mohamed Ali A. thank you!

JudLup Luna

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

JudLup Luna

there are random function class in java..

random r= new random();

 

Random random = new Random();

int ran=random.nextInt(100);

here is the code..

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

Raghavendra Ural

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi