English 中文(简体)
形成独特数目
原标题:Generating unique number
  • 时间:2010-11-10 10:31:09
  •  标签:
  • c++
  • c

页: 1 C++的数据类型。 我想为三个<代码>int32的组合创造独特的编号。 页: 1 C++。 例如,我有<编码>int iVal1,int iVal2int iVal3。 是否有这样的算法?

为避免问题混淆,我将重复这个问题。 基本上,我想产生一个独一无二的数字,有3个分类,因为我想把这一数目作为数据检索地图的关键。

任何建议? 感谢。

问题回答

You can use a good hash function.

将数字加在一起,其数量为3倍,而星号为96比特。

图1:0xDEADFACE;第2号:0xF00BA4;第3号:42

结果:0xDEADFACE00F00BA40000002A


Edit: 回归的典型用法构成新数字,用于扼杀。

#include <stdio.h>

/* writes a, b, c into dst
** dst must have enough space for the result */
char *concat3(char *dst, unsigned a, unsigned b, unsigned c) {
    sprintf(dst, "%08x%08x%08x", a, b, c);
    return dst;
}

/* usage */
int main(void) {
    char n3[25]; /* 25 = 3*8 for each integer + 1 for terminating null */
    concat3(n3, 0xDEADFACE, 0xF00BA4, 42);
    printf("result is 0x%s
", n3);
    return 0;
}

抽样调查

$ ./a.out
result is 0xdeadface00f00ba40000002a

计算方法: Linear congruential generator

从另外3个数字中得出一个数字的另一种方法是增加(至少一次工作)。

int n = x1 + x2 + x3

现在,<代码>n是一个新的独特编号。

#include <stdint.h>
#include <time.h>
#include <limits.h>

int main (int argc, char **argv) {
    uint32_t firstInt, secondInt, thirdInt;

    srandom(time(NULL)); /* seed RNG */

    firstInt = random(UINT32_MAX);
    secondInt = random(UINT32_MAX);
    thirdInt = random(UINT32_MAX);

    /* do something with unsigned ints */

    return 0;
}

灰心是正确的,即你不会从三个(甚至两个)愤怒者的厚颜色组合中获得独特的价值。 但是,很难知道你真的从你的问题的措辞来看什么。 如果你真的要有一个独一无二的数字,请查阅UUID

I think i understand. Venkata has a three numbers collection. Ex: 42, 35, 127. For each combination of these he needs a unique number. Ex:

int a[2] = {25, 63, 12};
int b[2] = {149, 28, 56};
GetNumber(a) != GetNumber(b)

和GetNumber(a)将永远=GetNumber(a),因此没有随机发电机。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签