Home  >  Article  >  Web Front-end  >  JavaScript creates random numbers and random pictures_javascript tips

JavaScript creates random numbers and random pictures_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:39:111254browse

Creating random numbers
①Since the creation of 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.
② Based on time, random numbers can also be generated. For example:
var now=new Date();
var number = now.getSeconds();
This will produce an integer from 0 to 59 based on the current time.
var now=new Date();
var number = now.getSeconds()C;
This will produce an integer from 0 to 42 based on the current time.
③Here is an excellent random number generator program that can be used in many fields.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

If you want to add it to your web page To use this random number generator, this code must be placed between the and tags.
To create a random floating point number, use rnd(); to create a random integer, for example, from 1 to 10, use rand(10).
In the following example of creating a random image, this random number generator is used to generate random numbers. Therefore, in order for the following example to work properly, make sure that the above code is included in your web page.
Create random pictures
Create random pictures. This is the simplest thing you can do with random numbers. In this example, we assume that there are 10 pictures, banner1.gif....banner10.gif, and we want to display these pictures randomly every time the document is loaded.

If the picture is not named in the form of a number, or the type or size of the picture is different, then you can also By checking the value of the random number, and then displaying the corresponding picture: The code is as follows:



The above describes how to randomly display images. In the same way, you can also use random numbers to create random text, forms, title ads, URLs, background colors and images, questionnaires and frames, etc. I won’t go into details here.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn