# Generate new key

**URL:** https://discourse.julialang.org/t/generate-new-key/6845
**Category:** General Usage
**Tags:** question
**Created:** [November 2, 2017, 3:40pm UTC](https://discourse.julialang.org/t/generate-new-key/6845 "2017-11-02T15:40:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [November 2, 2017, 3:40pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/1 "2017-11-02T15:40:05Z")

</div>

Is there a function/way to ask for a new unique key from an existing `Dict` that is not in already in the Dict? So I can safely assign to it knowing I’m not writing over something?  
Thanks!

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [November 2, 2017, 4:21pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/2 "2017-11-02T16:21:34Z")

</div>

What’s the key type? This works for numbers, at least:

```julia
keytype(di)

function new_key(di)
    local k
    while haskey(di, (k=rand(keytype(di));)) end
    k
end

new_key(Dict(1=>2))

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 2, 2017, 4:47pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/3 "2017-11-02T16:47:35Z")

</div>

As @cstjean said, you need a general mechanism that generates random variates of a type `T` that is not in a given set. Then you just call it on `keys(dict)`.

If the keys are sparse in `T`, just drawing until you get something new could be a quick & dirty method. Otherwise, if you have a total ordering, then the algorithms for sampling without replacement would be useful.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [November 2, 2017, 5:15pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/4 "2017-11-02T17:15:09Z")

</div>

I see.

> [@Tamas\_Papp](#):
>
> If the keys are sparse in T, just drawing until you get something new could be a quick & dirty method. Otherwise, if you have a total ordering, then the algorithms for sampling without replacement would be useful.

Could you elaborate on that? I don’t have any restrictions on the key type, so I can make it work faster, I’d gladly do that.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [November 2, 2017, 6:46pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/5 "2017-11-02T18:46:28Z")

</div>

Perhaps you can give a bit more info about your use case. Does the keys have any meaning? How would you store them?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [November 2, 2017, 7:14pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/6 "2017-11-02T19:14:53Z")

</div>

Absolutely! Here’s some background:

I’m writing (as a part of a bigger program) a script that allows users to log (biology) experiments. So the user can set the levels of the treatments (e.g. the light regime can be dark, normal, or bright), and add, remove, and edit individual runs (e.g. run #1 had normal light regime).  
I need to store a collection of these runs. I made a custom types to describe these runs. These custom types are initiated via a GUI, and stored in this collection. For the user to be able to delete or edit individual runs, I need to be able to refer to individual elements in that collection (things like `replace` and `∉`). Since the content of the runs themselves is not enough to identify them, I need to rely on some other identifier. I could hash the whole type instance, but! These runs are not immutable. They can change, for instance when another run is deleted the repetition number of all the consecutive runs decrease by 1. So hashing them won’t work.

This is probably too vague and detailed at the same time. I apologize for that. I can give you a whole lot more details if you like…

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [November 2, 2017, 7:17pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/7 "2017-11-02T19:17:13Z")

</div>

> [@cstjean](#):
>
> rand

While this initially looks like a suboptimal solution since it could potentially be slow, in practice it’s probably ultra fast: how often would `rand` generate the same element in a huge unique collection of say `Float64`? Pretty unlikely… Right?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 2, 2017, 7:35pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/8 "2017-11-02T19:35:19Z")

</div>

> [@yakir12](#):
>
> These runs are not immutable. They can change, for instance when another run is deleted the repetition number of all the consecutive runs decrease by 1. So hashing them won’t work.

Simply have a `counter::Int` which is incremented each time a run is added, making its current value the id for that run. Store runs in a `Dict{Int,T}`. Each new id will be unique, and it will be a long time before you run out of ids. You won’t need to worry about randomizing.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [November 2, 2017, 7:37pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/9 "2017-11-02T19:37:06Z")

</div>

> [@yakir12](#):
>
> While this initially looks like a suboptimal solution since it could potentially be slow, in practice it’s probably ultra fast: how often would rand generate the same element in a huge unique collection of say Float64?

Even if there’s a collision, it will generate a new one. I wouldn’t worry about it unless you have more than 2^60 runs. Also checkout `Base.Random.uuid1()`

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [November 6, 2017, 1:57pm UTC](https://discourse.julialang.org/t/generate-new-key/6845/10 "2017-11-06T13:57:54Z")

</div>

In reference to @Tamas_Papp, [CardinalDicts.jl](https://github.com/JeffreySarnoff/CardinalDicts.jl) is extra well suited for this.
