R: UUID Generator

uuid.gen {dplR} | R Documentation

## UUID Generator

### Description

Initializes and returns a generator of universally unique identifiers. Use the returned function repeatedly for creating one or more UUIDs, one per function call.

### Usage

    uuid.gen(more.state = "")

### Arguments

`more.state` | A `character` string for altering the state of the generator

### Details

This function returns a function (closure) which generates UUIDs. The state of that anonymous function is set when `uuid.gen` is called. The state consists of the following:

- System and user information (`Sys.info`)

- R version (`R.version`)

- Platform information (`.Platform`)

- Working directory

- Process ID of the R session

- Time when `uuid.gen` was called (precision of seconds or finer)

- The text in parameter `more.state`

The Pseudo Random Number Generator of R (see `.Random.seed`) is used in the generation of UUIDs. No initialization of the PRNG is done. Tampering with the state of the R PRNG while using a given UUID generator causes a risk of non-unique identifiers. Particularly, setting the state of the PRNG to the same value before two calls to the UUID generator guarantees two identical identifiers. If two UUID generators have a different state, it is not a problem to have the PRNG going through or starting from the same state with both generators.

The user is responsible for selecting a PRNG with a reasonable number of randomness. Usually, this doesn’t require any action. For example, any PRNG algorithm available in R works fine. However, the uniqueness of UUIDs can be destroyed by using a bad user-supplied PRNG.

The UUIDs produced by `uuid.gen` generators are Version 4 (random) with 122 random bits and 6 fixed bits. The UUID is presented as a `character` string of 32 hexadecimal digits and 4 hyphens:

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

where x is any hexadecimal digit and y is one of `"8"`, `"9"`, `"a"`, or `"b"`. Each x and y in the example is an independent variables (for all practical purposes); subscripts are omitted for clarity. The UUID generator gets 32 hex digits from the MD5 message digest algorithm by feeding it a string consisting of the constant generator state and 5 (pseudo) random numbers. After that, the 6 bits are fixed and the hyphens are added to form the final UUID.

### Value

A parameterless function which returns a single UUID (`character` string)

### Author(s)

Mikko Korpela

### References

Leach, P., Mealling, M., and Salz, R. (2005) A Universally Unique IDentifier (UUID) URN namespace. RFC 4122, RFC Editor. http://www.rfc-editor.org/rfc/rfc4122.txt.

### See Also

`digest`, `Random`

### Examples

    ## Normal use
    ug <- uuid.gen()
    uuids <- character(100)
    for(i in 1:100){
      uuids[i] <- ug()
    }
    length(unique(uuids)) == 100 # TRUE, UUIDs are unique with high probability

    ## Do NOT do the following unless you want non-unique IDs
    rs <- .Random.seed
    set.seed(0L)
    id1 <- ug()
    set.seed(0L)
    id2 <- ug()
    id1 != id2 # FALSE, The UUIDs are the same
    .Random.seed <- rs

    ## Strange usage pattern, but will probably produce unique IDs
    ug1 <- uuid.gen("1")
    set.seed(0L)
    id1 <- ug1()
    ug2 <- uuid.gen("2")
    set.seed(0L)
    id2 <- ug2()
    id1 != id2 # TRUE, The UUIDs are different with high probability
    .Random.seed <- rs

[Package dplR version 1.7.1 Index]