-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-gpt.js
43 lines (34 loc) · 1.1 KB
/
7-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
'use strict';
// Chat GPT version 4
// Prompt: Remove if statement with throw because it can be precalculated
// Reply:
// You're correct that the check can be precalculated if the bufSize and
// valueSize are constants.
// In that case, the code can be simplified as follows:
const crypto = require('node:crypto');
const bufSize = 4096;
const valueSize = 4;
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:
// I've removed the if statement with the throw since the bufSize and
// valueSize are constants and the condition (bufSize % valueSize !== 0)
// can be precalculated. In this case, it evaluates to false, so there's
// no need for the check in the code.
module.exports = { cryptoRandom };