This video shows the steps to generate pure random number in your Android App. This video initially explains about the pure/ true random number based on Hardware data and Pseudo Random Number generated based on some Algorithm. It referred to the below wiki page to discuss about the Random Numbers: https://en.wikipedia.org/wiki/Random_number_generation
To generate a true random number, in this video it consider sensors data which provides physical values which are almost impossible to predict based on time. The sensor data used in this video are of Ambient Temperature, Gravity, Gyroscope, pressure and proximity. However, audience is greatly encouraged to try other sensor data in their App based on their requirement. Please refer to the below link for getting sensors available in your Android Phone: https://developer.android.com/reference/android/hardware/Sensor
In the code, it is very simple. It just creates a sensor variable for each required data and then assigns it to a listener. Then it registers each sensors and listeners to collect the sensor data.
We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: programmerworld1990@gmail.com or visit us at: https://programmerworld.co
For more details on random numbers, please refer to the interesting article below:
Source Code:
package com.example.mypurerandomnumbergeneratorapp;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TextView textViewAmbientTemperature;
private TextView textViewGravity;
private TextView textViewGyroscope;
private TextView textViewPressure;
private TextView textViewProximity;
private TextView textViewCurrentDateTime;private TextView textViewFinalRandomNumber;
private double doubleAmbientTemperature;
private double doubleGravity;
private double doubleGyroscope;
private double doublePressure;
private double doubleProximity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);textViewAmbientTemperature = findViewById(R.id.textView);
textViewGravity = findViewById(R.id.textView2);
textViewGyroscope = findViewById(R.id.textView3);
textViewPressure = findViewById(R.id.textView4);
textViewProximity = findViewById(R.id.textView5);
textViewCurrentDateTime = findViewById(R.id.textView6);
textViewFinalRandomNumber = findViewById(R.id.textView7);SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor sensorAmbientTemperature = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
Sensor sensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
Sensor sensorGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Sensor sensorPressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
Sensor sensorProximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);SensorEventListener sensorEventListenerAmbitentTemperature = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
doubleAmbientTemperature = sensorEvent.values[0];
}@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};SensorEventListener sensorEventListenerGravity = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
doubleGravity = sensorEvent.values[0];
}@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};SensorEventListener sensorEventListenerGyroscope = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
doubleGyroscope = sensorEvent.values[0] + sensorEvent.values[1] + sensorEvent.values[2];
}@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
SensorEventListener sensorEventListenerPressure = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
doublePressure = sensorEvent.values[0];
}@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};SensorEventListener sensorEventListenerProximity = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
doubleProximity = sensorEvent.values[0];
}@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
sensorManager.registerListener(sensorEventListenerAmbitentTemperature, sensorAmbientTemperature, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListenerGravity, sensorGravity, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListenerGyroscope, sensorGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListenerPressure, sensorPressure, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListenerProximity, sensorProximity, SensorManager.SENSOR_DELAY_NORMAL);
}public void RandomNumberButton(View view){
double doubleCurrentDatetime = Calendar.getInstance().getTimeInMillis();
textViewAmbientTemperature.setText(Double.toString(doubleAmbientTemperature));
textViewGravity.setText(Double.toString(doubleGravity));
textViewGyroscope.setText(Double.toString(doubleGyroscope));
textViewPressure.setText(Double.toString(doublePressure));
textViewProximity.setText(Double.toString(doubleProximity));
textViewCurrentDateTime.setText(Double.toString(doubleCurrentDatetime));double doubleFinalRandomNumber = (doubleAmbientTemperature + 10*doubleGravity + 100*doubleGyroscope + doublePressure + doubleProximity + doubleCurrentDatetime)%1000;
textViewFinalRandomNumber.setText(Double.toString(doubleFinalRandomNumber));
}
}
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="@string/ambient_temperatrure" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="80dp" android:text="@string/gravity" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="136dp" android:text="@string/gyroscope" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView4" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="204dp" android:text="@string/pressure" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView5" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="264dp" android:text="@string/proximity" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView6" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="320dp" android:text="@string/current_date_and_time" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView7" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginStart="52dp" android:layout_marginTop="428dp" android:text="@string/my_final_random_number" android:textAlignment="center" android:textSize="36sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="28dp" android:onClick="RandomNumberButton" android:text="@string/random_number_generatror" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView6" /> </androidx.constraintlayout.widget.ConstraintLayout>