# Issue about generate random string Panicked at 'could not initialize thread\_rng: getrandom: this target is not supported'

**URL:** <https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198>\
**Category:** Developers\
**Tags:** Bug\
**Created:** [August 31, 2022, 12:45am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198 "2022-08-31T00:45:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cs2022](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cs2022/32/5273_2.png) [@cs2022](https://forum.dfinity.org/u/cs2022)\
**Post date:** [August 31, 2022, 12:45am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/1 "2022-08-31T00:45:45Z")

</div>

Reject text: Canister ryjl3-tyaaa-aaaaa-abbba-cai trapped explicitly: Panicked at ‘could not initialize thread\_rng: getrandom: this target is not supported’, /Users/xxxx/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.7.3/src/rngs/thread.rs:65:17

dependencies:  
rand = “0.7.3”  
getrandom = { version = “0.2.6”, features = [“js”]}

“dfx”: “0.9.3”,

I refered to previous solution, it doesn’t work. Does anyone know how to solve it?

---

<div class="post-metadata">

**Author:** ![AdamS](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/adams/32/5248_2.png) [@AdamS](https://forum.dfinity.org/u/AdamS)\
**Post date:** [September 1, 2022, 3:05pm UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/2 "2022-09-01T15:05:48Z")

</div>

features = [“js”] works in a JS environment; it does not work in a freestanding WASM environment. What you instead do is use the `register_custom_getrandom!` macro to register a CSPRNG you seeded through `ic0.raw_rand`. (I am curious what previous solution you refer to, that wasn’t that.)

---

<div class="post-metadata">

**Author:** ![cs2022](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cs2022/32/5273_2.png) [@cs2022](https://forum.dfinity.org/u/cs2022)\
**Post date:** [September 13, 2022, 1:46am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/3 "2022-09-13T01:46:03Z")

</div>

I tried higher rand version

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [September 13, 2022, 2:37am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/4 "2022-09-13T02:37:39Z")

</div>

> [@AdamS](#):
>
> What you instead do is use the `register_custom_getrandom!` macro to register a CSPRNG you seeded through `ic0.raw_rand`

I briefly looked into this at some point and assumed it wouldn’t work because the [docs for `register_custom_getrandom`](https://docs.rs/getrandom/latest/getrandom/macro.register_custom_getrandom.html) said:

> The function to register must have the same signature as [`getrandom::getrandom`](https://docs.rs/getrandom/latest/getrandom/fn.getrandom.html)

and that function isn’t `async` so I don’t understand how `raw_rand` can be called.

@AdamS if you have advice on how to make this work it would be really helpful.

---

<div class="post-metadata">

**Author:** ![AdamS](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/adams/32/5248_2.png) [@AdamS](https://forum.dfinity.org/u/AdamS)\
**Post date:** [September 13, 2022, 4:48pm UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/5 "2022-09-13T16:48:54Z")

</div>

Right, like I said you’d _seed_ an RNG of your choice with raw\_rand. Once created and stored in a static, this RNG can be used synchronously.

---

<div class="post-metadata">

**Author:** ![apotheosis](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/apotheosis/32/24516_2.png) [@apotheosis](https://forum.dfinity.org/u/apotheosis)\
**Post date:** [January 30, 2023, 2:02am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/6 "2023-01-30T02:02:47Z")

</div>

Does anyone have an example of this working?

```auto
use getrandom::register_custom_getrandom;
fn custom_getrandom(buf: &mut [u8]) -> Result<(), getrandom::Error> {
    // What goes here?
    return Ok(());
}

register_custom_getrandom!(custom_getrandom);

```

---

<div class="post-metadata">

**Author:** ![apotheosis](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/apotheosis/32/24516_2.png) [@apotheosis](https://forum.dfinity.org/u/apotheosis)\
**Post date:** [January 30, 2023, 4:46am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/7 "2023-01-30T04:46:59Z")

</div>

I did this:

```auto
use getrandom::register_custom_getrandom;
fn custom_getrandom(buf: &mut [u8]) -> Result<(), getrandom::Error> {
    let mut rng = StdRng::seed_from_u64(123);
    rng.fill_bytes(buf);
    return Ok(());
}

```

---

<div class="post-metadata">

**Author:** ![AdamS](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/adams/32/5248_2.png) [@AdamS](https://forum.dfinity.org/u/AdamS)\
**Post date:** [February 1, 2023, 4:40pm UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/8 "2023-02-01T16:40:23Z")

</div>

This will return the same numbers every time. What you can do instead is _seed_ an RNG of your choice with raw\_rand. Once created and stored in a static, this RNG can be used synchronously. For example:

```rust
thread_local! {
    static RNG: RefCell<Option<StdRng>> = RefCell::new(None);
}
#[init]
fn init() {
    ic_cdk::timer::set_timer(Duration::ZERO, || ic_cdk::spawn(async {
        let (seed,): ([u8; 32],) = ic_cdk::call(Principal::management_canister(), "raw_rand", ()).await.unwrap();
        RNG.with(|rng| *rng.borrow_mut() = Some(StdRng::from_seed(seed)));
    }));
}
fn custom_getrandom(buf: &mut [u8]) -> Result<(), getrandom::Error> {
    RNG.with(|rng| rng.borrow_mut().as_mut().unwrap().fill_bytes(buf));
    Ok(())
}

```

---

<div class="post-metadata">

**Author:** ![apotheosis](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/apotheosis/32/24516_2.png) [@apotheosis](https://forum.dfinity.org/u/apotheosis)\
**Post date:** [February 2, 2023, 3:38am UTC](https://forum.dfinity.org/t/issue-about-generate-random-string-panicked-at-could-not-initialize-thread-rng-getrandom-this-target-is-not-supported/15198/9 "2023-02-02T03:38:59Z")

</div>

Thank you, this is what I was looking for! 🙏
