-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-gpt.js
47 lines (37 loc) · 1.12 KB
/
5-gpt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
// Chat GPT version 4
// Prompt:
// - We do not need factory, just define object and combine state and next()
// function to this single instance
// Reply:
// Here's the code with the requested changes:
const crypto = require('node:crypto');
const bufSize = 4096;
const valueSize = 4;
if (bufSize % valueSize !== 0) {
throw new RangeError('buffer size must be a multiple of value size');
}
const randPrefetcher = {
buf: crypto.randomBytes(bufSize),
pos: 0,
vsz: valueSize,
next() {
if (this.pos === this.buf.length) {
this.pos = 0;
crypto.randomFillSync(this.buf);
}
const end = this.pos + this.vsz;
const buf = this.buf.slice(this.pos, end);
this.pos = end;
return buf;
},
};
const UINT32_MAX = 0xffffffff;
const cryptoRandom = () =>
randPrefetcher.next().readUInt32LE(0) / (UINT32_MAX + 1);
// Chat GPT comments:
// In this version of the code:
// - Removed the factory function and directly defined the randPrefetcher
// object with its state and next() method.
// - Removed the cryptoPrefetcher function as it's no longer needed.
module.exports = { cryptoRandom };