React Native Generate Random Number Between 1 to 100 in Android iOS

The JavaScript inbuilt function Math.floor() and Math.random() function is used in this tutorial. The Math.floor() function is used to round the given number to its downward integer value. The Math.random() function is used to Generate Random Number to any range in Point Float value. So we would use both Math.floor() and Math.random() function together to generate random number between given range. User can himself define the range like i did in this tutorial and the number will only be generated between given range.

Contents in this project Generate Random Number Between 1 to 100 in React Native :

1. Import StyleSheet, View, Button and Text component.

2. Create constructor() in your project. Now we would make a State named as NumberHolder.

3. Create a function named as GenerateRandomNumber() . Inside this function we would Generate Random Number using Math.floor() and Math.random() function and store the generated number into NumberHolder State.

4. Create a Root View, Text and Button component in render’s return block. The Text component is used to show the Random number.

5. Create the Style :

6. Complete source code for App.js File :

Screenshot in Android device:

Random Number Screenshot in iOS application :

Leave a Reply

Your email address will not be published.