读书人

如何得到一个随机数

发布时间: 2012-10-14 14:55:08 作者: rapoo

怎么得到一个随机数?
给设备设置MAC地址,需要每台设备MAC地址不同,怎么才能得到随机数?
【除了和时间关联,因为没有时钟芯片,取的时间总是相同,所以用rand产生的随机数也相同。】

[解决办法]
srand的时候,尽量使种子值不同,最简单就是每次对上一个种子值增大等变换
[解决办法]
用随机数怎么可能保证不同?万一碰上了呢?
[解决办法]
CoCreateGuid
Creates a GUID, a unique 128-bit integer used for CLSIDs and interface identifiers.

HRESULT CoCreateGuid(
GUID *pguid //Pointer to the GUID on return
);

Parameter
pguid
[out] Pointer to the requested GUID on return.
Return Value
S_OK
The GUID was successfully created.
Win32 errors are returned byUuidCreate but wrapped as an HRESULT.

Remarks
The CoCreateGuid function calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use the CoCreateGuid function when you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.To a very high degree of certainty, this function returns a unique value no other invocation, on the same or any other system (networked or not), should return the same value.

QuickInfo
Windows NT: Use version 3.1 or later.
Windows: Use Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in objbase.h.
Import Library: Included as a resource in ole32.dll.

See Also
UuidCreate





[解决办法]
思路上就有问题
任何附加条件的东西都不能叫做随机,数与数之间完全没有任何关系,可能完全不同也可能完全相同,这才叫随机

我用过的设备用于区分彼此的地址都是通电状态下人工设置后保存,每次上电时读取

[解决办法]

探讨
CoCreateGuid
Creates a GUID, a unique 128-bit integer used for CLSIDs and interface identifiers.

HRESULT CoCreateGuid(
GUID *pguid //Pointer to the GUID on return
);

Parameter
pguid
[ou……

[解决办法]
一次撒种子,多次取值就可以了

读书人网 >C++

热点推荐