Abdulrahman Azmy
Abdulrahman Azmy

Reputation: 69

How to generate a random number between 1 and a million?

I want to make a function the generates a random number from 1 to million .. and put it in a form of

"You have won [ random value ] points. Congratulations."

and somehow save it into variable "x" that i can call in a text box so in the text box value which like:

<input class="textbox1" type="text" readonly value="[X]"> 
<p> [X] </p>
<a href="mywebsite" data-text="[X]"></a>

So i can use it in different places, how could I do it ?

Upvotes: 2

Views: 6198

Answers (4)

Nieck
Nieck

Reputation: 1646

In Javascript you can use:

Math.floor((Math.random() * 1000000) + 1);

This will generate a number between 1 and 1 million.

A snippet to test:

console.log(Math.floor((Math.random() * 1000000) + 1));

Upvotes: 6

HarisH Sharma
HarisH Sharma

Reputation: 1267

Use this if want better random numbers:

echo mt_rand(5, 15);

or visit here: http://php.net/manual/en/function.mt-rand.php

Upvotes: 1

R P
R P

Reputation: 169

use php function rand()

rand(1, 1000000);

Upvotes: 2

Gynteniuxas
Gynteniuxas

Reputation: 7103

Generate random number between 1 and 1 million and save in $randnumber; Then echo the sentence. Finally, show that number in HTML by inserting PHP code:

//...
$randnumber = rand(1, 1000000);

echo "You have won ".$randnumber." points. Congratulations.";
?>

<input class="textbox1" type="text" value="<?= $randnumber ?>" readonly> 
<p> <?= $randnumber ?> </p>
<a href="mywebsite" data-text="<?= $randnumber ?>">Text</a>

I think there are no syntax errors.

Upvotes: 18

Related Questions