A string random animation digital _javascript technique with JS randomly generating alphanumeric combinations

Source: Internet
Author: User
Tags numeric value rand

JS Random animation generates a set of random numbers


Online Preview Click to download

Effect Description:

Only one index.html file in the attachment is valid

It contains CSS and two parts of HTML

Several random numbers generated by pure JS

Do not repeat each time, click the button and then switch again

How to use:

1, the introduction of CSS style to your Web page

2, the body of the code part of the copy to the place you need to

JS generates random strings of alphanumeric combinations

Preface

A recent requirement is to generate a random string of 3-32-digit alphanumeric combinations, and another to generate a 43-bit random string.

Method One

A wonderful writing.

Math.random (). toString. substr (2);

Output results

Explain

Very interesting, studied, basically after the parameters of the ToString can be 2-36 of any integer, do not write the default is 10 (that is, decimal), at this time the return value is that random number.

If even, the returned numeric string is short and, if odd, returns a large length representation value.
If <10 are all numbers, >10 will contain letters.
So if you want to get a long string of random characters, you need to use a > 10 and an odd number of parameters, in addition to the length of their own use slice (2,n) intercept!

Method Two

This is a lot of implementation methods, because the last one of the writing does not meet the requirements so write the next one, welcome to pat Bricks.

Address

https://gist.github.com/xuanfeng/b23ab28ab412254e1594

Code

*
* * * * Randomword produces arbitrary length random alphanumeric combinations
* * randomflag-whether arbitrary length min-any length of the minimum bit [fixed digits] max-arbitrary length maximum bit
* * Xuanfeng 2014-08-28
*
 
/function Randomword (randomflag, Min, max) {
 var str = "",
 range = min,
 arr = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' " 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ' , ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ';
 
 Randomly produces
 if (randomflag) {
 range = Math.Round (Math.random () * (max-min)) + min;
 }
 for (var i=0 i<range; i++) {
 pos = Math.Round (Math.random () * (arr.length-1));
 STR + + Arr[pos];
 }
 return str;
}

How to use

Generate 3-32-bit random string: Randomword (True, 3, 32)

Generate 43-bit random string: Randomword (False, 43)

Several usages of JS generating random numbers

<script> 
function Getrandomnum (Min,max)
{ 
var Range = max-min; 
var Rand = Math.random (); 
Return (Min + math.round (Rand * Range)); 
var num = getrandomnum (1,10); 
alert (num); 
</script>
var chars = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ' , ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ';
function generatemixed (n) {
 var res = "";
 for (var i = 0; i < n; i + +) {
  var id = math.ceil (math.random () *35);
  Res + + Chars[id];
 }
 return res;
}

1.math.random (); The result is a random number between 0-1 (including 0, excluding 1)

2.math.floor (num); Parameter num is a numeric value, and the function result is the integer portion of Num.

3.math.round (num); The parameter num is a numeric value, and the function result is the integer rounded by Num.

Math: A Mathematical object that provides a mathematical calculation of the data.

Math.random (); Returns a random number between 0 and 1 (including 0, excluding 1).

Math.ceil (n); Returns the smallest integer greater than or equal to N.

With Math.ceil (Math.random () *10), the probability of taking 0 is minimal when the random integer 1 to 10 is obtained.

Math.Round (n); Returns the value of the integer after n rounding.

With Math.Round (Math.random ()), a random integer of 0 to 1 can be obtained evenly.

With Math.Round (Math.random () *10), a random integer of 0 to 10 is obtained in a basic equilibrium, with half the probability of obtaining a minimum value of 0 and a maximum of 10.

Math.floor (n); Returns the largest integer less than or equal to N.

With Math.floor (Math.random () *10), a random integer of 0 to 9 can be obtained evenly.

This article to share JS generated random number of related content, to learn more about the JS random number of questions please continue to pay attention to this site, our site every day there are new content updates.

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.