JavaScript create random numbers and random pictures _javascript tips

Source: Internet
Author: User
Tags generator rand
Create a random number
① since JavaScript, many browsers have built-in random number generation methods. For example:
var number = Math.random ();
This method produces a floating-point number between 0 and 1.
② is based on time and can also produce random numbers. For example:
var now=new Date ();
var number = Now.getseconds ();
This will result in a 0 to 59 integer based on the current time.
var now=new Date ();
var number = Now.getseconds ()%43;
This will result in a 0 to 42 integer based on the current time.
③ here introduces a fairly excellent random number generator program that can be applied in many fields.
<script language= "JavaScript" ><!--rnd.today=new Date (); Rnd.seed=rnd.today.gettime ();     function rnd () {rnd.seed = (rnd.seed*9301+49297)% 233280; Return rnd.seed/(233280.0); }; function rand (number) {return Math.ceil (rnd () *number); }; End of the randomizer. --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

If you want to use the random number generator on your Web page, you must place this code between the <HEAD> and </HEAD> tags.
To create a random floating-point number, use Rnd (), and when you want to create a random integer, for example, 1 to 10 o'clock, use RAND (10).
The following example of creating a random picture is using this random number generator to produce random numbers, so to make sure that the following examples work, you need to ensure that your page contains the above code.
Create a random picture
Create random pictures, which is the easiest thing to do with random numbers. In this example, we assume that there are 10 images, banner1.gif.....banner10.gif, and that we want to randomly display these pictures whenever the document is loaded.
<script language= "JavaScript" ><!--
document.write (' ');
--></script>
If the picture is not named as a number, or if the type or size of the picture is different, you can also check the value of the random number and display the corresponding picture:
Copy Code code as follows:

<script language= "JavaScript" ><!--
var number = rand (10);
if (number = = 1) {
var picture = "One.gif"; var width = 400;var height = 40;
}
else if (number = = 2) {
var picture = "Two.jpg"; var width = 200;var height = 20;
}
...
...
else if (number = = 9) {
var picture = "Nine.gif"; var width = 450;var height = 450;
}
else if (number = = 10) {
var picture = "Ten.jpg"; var width = 40;var height = 400;
}
document.write (' ');
--></script>

The above describes how to display pictures randomly, the same way, you can also use random numbers to create random text, forms, title ads, URLs, background colors and images, questionnaires and frames, and so on, there is no more to say.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.