# How to use Random

**URL:** <https://forum.dfinity.org/t/how-to-use-random/2008>\
**Category:** Language Support\
**Created:** [February 19, 2021, 9:13am UTC](https://forum.dfinity.org/t/how-to-use-random/2008 "2021-02-19T09:13:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 19, 2021, 9:13am UTC](https://forum.dfinity.org/t/how-to-use-random/2008/1 "2021-02-19T09:13:19Z")

</div>

I’m trying to do something very simple. Generate a random number…  
I can’t understand from the docs how to use it.  
I read these docs here [Random :: Internet Computer](https://sdk.dfinity.org/docs/base-libraries/random)  
But I get all kind of errors which I don’t really understand…  
Can someone give me an example please?

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [February 19, 2021, 10:23am UTC](https://forum.dfinity.org/t/how-to-use-random/2008/2 "2021-02-19T10:23:58Z")

</div>

Hi,

So in need for uuid I came with this. Not sure it’s what you need but hopefully it helps.

If you don’t need uuid word8ToText should work for you.

public func getBytes(n : Nat) : async [Word8] {  
let m = (n + 31) / 32;  
var chunk = Iter.fromArray();  
let beacons = Array.init(m, chunk);  
for (i in Iter.range(0, m - 1)) {  
beacons[i] := (await Random.blob()).bytes();  
};  
let source = Iter.fromArrayMut(beacons);  
Array.tabulate(n, func \_ {  
switch (chunk.next()) {  
case (?x) x;  
case (null) {  
chunk := Option.unwrap(source.next());  
Option.unwrap(chunk.next())  
}  
}  
})  
};

public func uuid() : async Text {  
var uuid : Text = “”;  
uuid #= Text.concat(word8ToText(await getBytes(2)) , “-”);  
uuid #= Text.concat(word8ToText(await getBytes(2)) , “-”);  
uuid #= Text.concat(word8ToText(await getBytes(2)) , “-”);  
uuid #= Text.concat(word8ToText(await getBytes(2)) , “-”);  
uuid #= word8ToText(await getBytes(6));

```
uuid;

```

};

private func word8ToText(i : [Word8]): Text {  
var str: Text = “”;  
Array.foldLeft\<Word8, Text\>(i, str, func(str, i) { Text.concat(str, Nat.toText(Prim.word8ToNat(i))) });  
};

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [February 19, 2021, 10:24am UTC](https://forum.dfinity.org/t/how-to-use-random/2008/3 "2021-02-19T10:24:17Z")

</div>

> But I get all kind of errors which I don’t really understand…

Even if you get errors you don’t understand, it helps to include them in your post, in case others can make sense of them.

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabriel/32/546_2.png) [@Gabriel](https://forum.dfinity.org/u/Gabriel)\
**Post date:** [February 19, 2021, 10:33am UTC](https://forum.dfinity.org/t/how-to-use-random/2008/4 "2021-02-19T10:33:09Z")

</div>

Seems like everytime I try to use 3 backticks to format the code I get a 403 forbidden…

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [February 19, 2021, 10:37am UTC](https://forum.dfinity.org/t/how-to-use-random/2008/5 "2021-02-19T10:37:51Z")

</div>

I think it’s more the edit feature that’s broken?

```auto
because I can do this in a fresh post

```

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 19, 2021, 2:21pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/7 "2021-02-19T14:21:51Z")

</div>

I deleted my reply by mistake not sure how to revert it I will repost

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 19, 2021, 2:26pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/8 "2021-02-19T14:26:53Z")

</div>

I managed to get it working. I was confused about how to use the Finite class but it seems I get it now.

```
import Random "mo:base/Random";

actor {

    let r = Random.Finite("test");
    public func testRandom() : async ?Bool {
        return r.coin();
    }
}
```

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 19, 2021, 4:48pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/9 "2021-02-19T16:48:06Z")

</div>

Another question. is there anything like rand() from C or Random.nextFloat from java or Math.random() from javascript. My quest with Motoko Random started because I want to shuffle an array…

---

<div class="post-metadata">

**Author:** ![Emmo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/emmo/32/831_2.png) [@Emmo](https://forum.dfinity.org/u/Emmo)\
**Post date:** [February 25, 2021, 9:29pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/10 "2021-02-25T21:29:16Z")

</div>

What does the return of this function look like?

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 25, 2021, 9:35pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/11 "2021-02-25T21:35:15Z")

</div>

a float between 0 and 1. Math.random() is using xorshift128+

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [February 25, 2021, 11:09pm UTC](https://forum.dfinity.org/t/how-to-use-random/2008/12 "2021-02-25T23:09:22Z")

</div>

I gave it a go here [xorshift128plus.mo · GitHub](https://gist.github.com/chchrist/927b0c8ffe36a52b11522f470b81f216)  
Seems to be generating pseudo random numbers but since I’m new Motoko learner I’m pretty sure it can be written better.
