Last change
on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
721 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var crypto = require('crypto');
|
---|
| 4 |
|
---|
| 5 | // This string has length 32, a power of 2, so the modulus doesn't introduce a
|
---|
| 6 | // bias.
|
---|
| 7 | var _randomStringChars = 'abcdefghijklmnopqrstuvwxyz012345';
|
---|
| 8 | module.exports = {
|
---|
| 9 | string: function(length) {
|
---|
| 10 | var max = _randomStringChars.length;
|
---|
| 11 | var bytes = crypto.randomBytes(length);
|
---|
| 12 | var ret = [];
|
---|
| 13 | for (var i = 0; i < length; i++) {
|
---|
| 14 | ret.push(_randomStringChars.substr(bytes[i] % max, 1));
|
---|
| 15 | }
|
---|
| 16 | return ret.join('');
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | , number: function(max) {
|
---|
| 20 | return Math.floor(Math.random() * max);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | , numberString: function(max) {
|
---|
| 24 | var t = ('' + (max - 1)).length;
|
---|
| 25 | var p = new Array(t + 1).join('0');
|
---|
| 26 | return (p + this.number(max)).slice(-t);
|
---|
| 27 | }
|
---|
| 28 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.