Skip to content

Commit 0b003cf

Browse files
makiuchi-dburgrp
authored andcommitted
runtime: avoid fixed math/rand sequence on RP2040/RP2350 (#5124)
* runtime: avoid fixed math/rand sequence on RP2040/RP2350 * fix typo
1 parent 345de25 commit 0b003cf

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/runtime/rand_norng.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build baremetal && !(nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt))
1+
//go:build baremetal && !(nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt) || rp2040 || rp2350)
22

33
package runtime
44

src/runtime/rand_rp2.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build rp2040 || rp2350
2+
3+
package runtime
4+
5+
import "machine"
6+
7+
var hardwareRandValue uint64
8+
9+
func hardwareRand() (n uint64, ok bool) {
10+
if hardwareRandValue == 0 {
11+
n1, _ := machine.GetRNG()
12+
n2, _ := machine.GetRNG()
13+
hardwareRandValue = uint64(n1)<<32 | uint64(n2)
14+
}
15+
16+
// Return ok=false to keep using fastrand64(),
17+
// with hardwareRandVal used only as its initial random state.
18+
19+
return hardwareRandValue, false
20+
}

0 commit comments

Comments
 (0)