| 1 | // Copyright (c) 2022 John Lloyd. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | |
| 5 | module rand |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | fn C.arc4random_buf(p &u8, n usize) |
| 10 | |
| 11 | // read returns an array of `bytes_needed` random bytes read from the OS. |
| 12 | pub fn read(bytes_needed int) ![]u8 { |
| 13 | mut buffer := unsafe { malloc_noscan(bytes_needed) } |
| 14 | C.arc4random_buf(buffer, bytes_needed) |
| 15 | return unsafe { buffer.vbytes(bytes_needed) } |
| 16 | } |
| 17 | |