# Random Principals in Motoko - For Testing

**URL:** <https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [August 6, 2021, 9:42pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338 "2021-08-06T21:42:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [August 6, 2021, 9:42pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/1 "2021-08-06T21:42:30Z")

</div>

I’m trying to decipher a Principal structure so that I can create some random principals in my tests.

```auto
import Principal "mo:base/Principal";
import Blob "mo:base/Blob";
import Debug "mo:base/Debug";

actor Echo {

  // Say the given phase.
  public shared(msg) func test() : async ([Nat8],Text) {
    Debug.print(debug_show(Principal.fromActor(Echo)));
    return (
        Blob.toArray(
            Principal.toBlob(
                Principal.fromActor(Echo)
            )
        ), Principal.toText(Principal.fromActor(Echo)));
  };
};

```

This outputs something like (vec {0; 0; 0; 0; 0; 48; 0; 128; 1; 1}, “zwbmv-jyaaa-aaaab-qacaa-cai”). the last two numbers always seem to be 1 and only the 3rd to last and 5th to last seem to change much). What is the best way to be doing this? I seem to remember seeing the structure at some point. Is it 8 bytes and then two bytes of a check sum? How do I produce the check sum? I see libraries out there like [motoko-crc/CRC8.mo at master · enzoh/motoko-crc · GitHub](https://github.com/enzoh/motoko-crc/blob/master/src/CRC8.mo) but they look old. Can I just swap out Word8 for Nat8 and spend a hash to a crc check of the hash like in [Cyclic Redundancy Checks](https://forum.dfinity.org/t/cyclic-redundancy-checks/676)?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [August 6, 2021, 11:22pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/2 "2021-08-06T23:22:53Z")

</div>

The principal format is speced here: [The Internet Computer Interface Specification :: Internet Computer](https://sdk.dfinity.org/docs/interface-spec/index.html#principal)

If you need some random identities, you can use `ic-repl`'s identity command. For example, here is the test script we use for CanCan: [cancan/accessControl.test.sh at main · dfinity/cancan · GitHub](https://github.com/dfinity/cancan/blob/main/service/tests/accessControl.test.sh)

---

<div class="post-metadata">

**Author:** ![jorgenbuilder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jorgenbuilder/32/22166_2.png) [@jorgenbuilder](https://forum.dfinity.org/u/jorgenbuilder)\
**Post date:** [December 19, 2022, 5:28pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/3 "2022-12-19T17:28:56Z")

</div>

I had the problem of needing a bunch of principals for motoko unit testing today. I came up with this, anyone got another approach?

```auto
let principals = Array.tabulate<Principal>(
  10,
  func(i) {
    Principal.fromBlob(Blob.fromArray([Nat8.fromNat(i)]));
  },
);

```

---

<div class="post-metadata">

**Author:** ![mparikh](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mparikh](https://forum.dfinity.org/u/mparikh)\
**Post date:** [December 19, 2022, 8:22pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/4 "2022-12-19T20:22:49Z")

</div>

off topic in Motoko… but in rust

#[ic\_cdk\_macros::update]

async fn getrandomprincipal() → String {  
let bytes = raw\_rand().await;  
if bytes.is\_ok() {  
let bytes = bytes.unwrap();  
let vbytes = bytes.0;  
let slice = &vbytes[0…29];  
let p = Principal::from\_slice(slice);  
return format!(“{}”, p.to\_text());  
}  
format!(“NPT FOUND”)  
}

---

<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:** [December 23, 2022, 6:37am UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/5 "2022-12-23T06:37:22Z")

</div>

> [@chenyan](#):
>
> If you need some random identities, you can use `ic-repl`’s identity command. For example, here is the test script we use for CanCan: [cancan/accessControl.test.sh at main · dfinity/cancan · GitHub](https://github.com/dfinity/cancan/blob/main/service/tests/accessControl.test.sh)

Is there a way to generate an identity/principal without switching to it?

As a workaround I’m immediately switching back to the identity I was previously using.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [December 23, 2022, 5:59pm UTC](https://forum.dfinity.org/t/random-principals-in-motoko-for-testing/6338/6 "2022-12-23T17:59:10Z")

</div>

You can create a bunch of identities first, and then choose the one you want to use, e.g., `identity alice; identity bob; identity cathy; ...`

We don’t have a way to export identities yet. If you use dfx to create a bunch of pem files, you can use `identity alice "a.pem"` when you want to use it.
