```
Math.random().toString(36)
```

i want to achieve this js code in rust .

but I don't know how to translate float point by 36 credit

i found this library of `radix_fmt`

but not support `f64`

```
Math.random().toString(36)
```

i want to achieve this js code in rust .

but I don't know how to translate float point by 36 credit

i found this library of `radix_fmt`

but not support `f64`

I don't understand what you mean by "credit". All this code seems to do is take a random number between 0 and 1 and output a base-36 string representation of it. The JS implementation I have access to (Firefox) performs the conversion to 10 or 11 significant figures (about 57 bits, which is understandable given the 53-bit precision of `f64`

). Since the number is always between 0 and 1, the first two characters are always `0.`

and then a 10 or 11 character random string follows.

This can be easily done using the `rand`

crate, by choosing 11 of the 36 possible digits (Playground):

```
static BASE_36_DIGITS: &[u8; 36] = b"0123456789abcdefghijklmnopqrstuvwxyz";
fn random_base36() -> String {
let mut rng = thread_rng();
"0.".chars()
.chain(BASE_36_DIGITS.choose_multiple(&mut rng, 11).copied().map(char::from))
.collect()
}
```

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.