Vikas Kulkarni
Vikas Kulkarni

Reputation: 87

Generate unique sequence ID

how to Generate a unique sequence ID to be stored in unsigned long ? Getting seconds elapsed after 1970 would have been good idea but requirement is within a second the id might be updated , so second wont be unique !

Upvotes: 2

Views: 2752

Answers (5)

Rob Beer
Rob Beer

Reputation: 199

How you generate the unique_ids is a useful question - but you seem to be making a counter productive assumption about when you generate them!

My point is that you do not need to generate these unique id's at the time of creating your rows, because they are essentially independent of the data being inserted.

What I do is pre-generate unique id's for future use, that way I can take my own sweet time and absolutely guarantee they are unique, and there's no processing to be done at the time of the insert.

For example I have an orders table with order_id in it. This id is generated on the fly when the user enters the order, incrementally 1,2,3 etc forever. The user does not need to see this internal id.

Then I have another table - unique_ids with (order_id, unique_id). I have a routine that runs every night which pre-loads this table with enough unique_id rows to more than cover the orders that might be inserted in the next 24 hours. (If I ever get 10000 orders in one day I'll have a problem - but that would be a good problem to have!)

This approach guarantees uniqueness and takes any processing load away from the insert transaction and into the batch routine, where it does not affect the user.

Upvotes: 0

selbie
selbie

Reputation: 104474

If your requirements are to pick something pseudo random, fast, reliably unique, and not used for requirement for crytographic security purposes, I offer up the following

On Windows X86:

__rdtsc() - is about as good of a sequential number as it gets. XOR the upper 32-bits of the return value with the lower 32-bits. As the lower 32-bits will cycle every couple of seconds

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <intrin.h>
#include <stdint.h>

uint32_t GetPseudoRandomNumber()
{
    uint64_t t = _time64(NULL);
    uint64_t cpu = __rdtsc();
    uint32_t result;
    cpu = cpu ^ t;
    result = (cpu >> 32) ^ (cpu & 0xffffffff);
    return result;
}

uint32_t GetPseudoRandomNumber2()
{
    GUID guid = {};
    uint32_t* pValue = (uint32_t*)&guid;
    uint32_t result;

    CoCreateGuid(&guid);

    result = pValue[0] ^ pValue[1] ^ pValue[2] ^ pValue[3];

    return result;
}

Other sources of entropy include GetTickCount() (unique to the millisecond)

On Linux: Just read 4 bytes from /dev/urandom

Upvotes: 1

The Mask
The Mask

Reputation: 17427

Try use: srand ( time(NULL) );

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

Unless you provide more information about your requirements, it's going to be hard to come up with an answer besides:

unsigned long next_id = 0;

unsigned long new_id() {
    return next_id++;
}

Upvotes: 7

Petar Ivanov
Petar Ivanov

Reputation: 93000

use milliseconds elapsed after 1970

Upvotes: 0

Related Questions