Android random function

Android random function

Bild des Benutzers JudLup Luna

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
7 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.
Bild des Benutzers JudLup Luna

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
Bild des Benutzers iliyapolak

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.

Bild des Benutzers Sergey Kostrov

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

Bild des Benutzers Mohamed helmi b.

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.

Bild des Benutzers Mohamed Ali A.

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

Bild des Benutzers Ahmed BH

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

Melden Sie sich an, um einen Kommentar zu hinterlassen.