Hướng dẫn dùng generaterandom JavaScript

- Ở bài học trước, tôi đã giới thiệu sơ qua về phương thức random[] của đối tượng Math, nó dùng để tạo một số ngẫu nhiên trong khoảng từ 0 đến 1 [không bao gồm số 1]

Nội dung chính Show

  • 1] Tạo một số nguyên ngẫu nhiên
  • 2] Xây dựng hàm dùng để tạo số nguyên ngẫu nhiên
  • 1] Tạo một số nguyên ngẫu nhiên
  • 2] Xây dựng hàm dùng để tạo số nguyên ngẫu nhiên

- Tuy nhiên không dừng lại ở đó, nếu phương thức random[] được sử dụng kết hợp với các phương thức khác thì nó có thể tạo ra những số ngẫu nhiên đa dạng hơn.

- Ở bài hướng dẫn này, tôi sẽ giới thiệu đến bạn kỹ thuật để tạo một số ngẫu nhiên đa dạng hơn thông qua những ví dụ.

1] Tạo một số nguyên ngẫu nhiên

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 9


    Math.floor[Math.random[] * 10];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 10


    Math.floor[Math.random[] * 11];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 99


    Math.floor[Math.random[] * 100];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 100


    Math.floor[Math.random[] * 101];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 1 đến 10


    Math.floor[Math.random[] * 10] + 1;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 1 đến 100


    Math.floor[Math.random[] * 100] + 1;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 3 đến 7


    Math.floor[Math.random[] * 5] + 3;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 3 đến 9


    Math.floor[Math.random[] * 7] + 3;

Xem ví dụ

2] Xây dựng hàm dùng để tạo số nguyên ngẫu nhiên

- Ta thấy trong mỗi ví dụ phía trên, giá trị trả về luôn nằm trong một đoạn nhất định, ví dụ:

  • Từ 0 đến 9
  • Từ 1 đến 100
  • Từ 3 đến 7
  • ....

    ==> Điều đó thật hạn chế nếu như ta muốn tạo nhiều số nguyên trong nhiều đoạn khác nhau.

- Từ đây, để giải quyết vấn đề này thì chúng ta nên xây dựng một hàm tạo số nguyên ngẫu nhiên, chỉ với việc thay đổi giá trị của tham số là ta đã có thể xác định được một đoạn mới.

There is no best way to do this. You can do it any way you prefer, as long as the result suits your requirements. To illustrate, I've created many different examples, all which should provide the same end-result

Nội dung chính Show

  • Example 1: Generate Random Strings
  • Example 2: Generate Random Strings Using Built-in Methods
  • Random Posts

Most other answers on this page ignore the upper-case character requirement.

Here is my fastest solution and most readable. It basically does the same as the accepted solution, except it is a bit faster.

function readableRandomStringMaker[length] {
  for [var s=''; s.length < length; s += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.charAt[Math.random[]*62|0]];
  return s;
}
console.log[readableRandomStringMaker[length]];
// e3cbN

Here is a compact, recursive version which is much less readable:

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj

A more compact one-liner:

Array[5].fill[].map[[]=>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62]].join[""]
// 12oEZ

A variation of the above:

"     ".replaceAll[" ",[]=>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62]]

The most compact one-liner, but inefficient and unreadable - it adds random characters and removes illegal characters until length is l:

[[l,f=[p='']=>p.lengthf[]][5]

A cryptographically secure version, which is wasting entropy for compactness, and is a waste regardless because the generated string is so short:

[...crypto.getRandomValues[new Uint8Array[999]]].map[[c]=>String.fromCharCode[c].replace[/[^a-z0-9]/i,'']].join[""].substr[0,5]
// 8fzPq

Or, without the length-argument it is even shorter:

[[f=[p='']=>p.lengthf[]][]
// EV6c9

Then a bit more challenging - using a nameless recursive arrow function:

[[l,s=[[l]=>l--&&"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0]+[s[l]||""]]] => s[l]][5];
// qzal4

This is a "magic" variable which provides a random character every time you access it:

const c = new class { [Symbol.toPrimitive][] { return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] } };
console.log[c+c+c+c+c];
// AgMnz

A simpler variant of the above:

const c=[]=>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0];
c[]+c[]+c[]+c[]+c[];
// 6Qadw

Example 1: Generate Random Strings

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
0

Output

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
1

In the above example, the

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
4 method is used to generate random characters from the specified characters [A-Z, a-z, 0-9].

The

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
5 loop is used to loop through the number passed into the
const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
6 function. During each iteration, a random character is generated.

Example 2: Generate Random Strings Using Built-in Methods

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
2

Output

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
3

In the above example, built-in methods are used to generate random characters.

The

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
4 method generates the random number between 0 and 1.

In 

const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
8 method, 36 represents base 36. The
const compactRandomStringMaker = [length] => length-- && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62|0] + [compactRandomStringMaker[length]||""];
console.log[compactRandomStringMaker[5]];
// DVudj
8 represents digits beyond 9 by letters.

The

Array[5].fill[].map[[]=>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt[Math.random[]*62]].join[""]
// 12oEZ
0 method returns five characters.

Note: In the above examples, the output varies each time because random characters are generated at every execution.

Rate this post Gracias por ver el video no te olvides suscribirte para mas contenido comparte y dale me gusta…

Rate this post Video HD 1 phút xóa Virus tự động mở các trình duyệt website không mong muốn trên…

Rate this post Setting up Dahua SmartPSS v2.02 Tag: idmss for pc, Smart PSS, dahua Xem Thêm Bài Viết Mobile…

Rate this post This playlist has been updated! Please check: what is instanceof … Tag: javascript instanceof, [vid_tags] Xem Thêm…

Chủ Đề