本文整理汇总了C++中rand_s函数的典型用法代码示例。如果您正苦于以下问题:C++ rand_s函数的具体用法?C++ rand_s怎么用?C++ rand_s使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rand_s函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: urner_bang
void urner_bang(t_urner *x)
{
long rand;
long size = sysmem_ptrsize(x->table); // RETURNS SIZE OF POINTER IN BYTES
#ifdef WIN_VERSION
rand_s(&rand);
#else
rand = random();
#endif
rand = rand % size;
if (x->count == size) {
outlet_bang(x->bangout); // SEND A BANG WHEN WE'VE HIT MAXIMUM
return;
}
if (x->table[rand] != 0) { // NUMBER HAS ALREADY BEEN CHOSEN
do {
#ifdef WIN_VERSION
rand_s(&rand);
#else
rand = random();
#endif
rand = rand % size;
} while (x->table[rand] != 0);
}
// WE GOT A NUMBER
x->table[rand] = 1; // MARK THIS VALUE AS USED
x->count++; // INCREMENT OUR COUNT
outlet_int(x->out, rand);
}
示例2: D3DXMatrixRotationZ
void CElementManager::CreateCircle(const D3DXVECTOR3& pos, FLOAT intensity, const SMap *map,
FLOAT lastTime, FLOAT length, INT max, DWORD color, BOOL isPlayer)
{
D3DXVECTOR3 dir;
D3DXMATRIX trans;
if (intensity > 1.f) intensity = 1.f;
INT cnt = static_cast<INT>(max*intensity+0.5f);
D3DXMatrixRotationZ(&trans, 2*D3DX_PI/cnt);
UINT t;
do {
rand_s(&t);
dir.x = t%10-5.f;
rand_s(&t);
dir.y = t%10-5.f;
} while (dir.x == 0.f && dir.y == 0.f);
dir.z = 0.f;
D3DXVec3Normalize(&dir, &dir);
if (isPlayer)
for (INT i(0); i < cnt; ++i) {
D3DXVec3TransformNormal(&dir, &dir, &trans);
m_pEcho.push_back(new CLine(pos, dir,
intensity, map, intensity <= 2.f/9, lastTime, length, color));
}
else
for (INT i(0); i < cnt; ++i) {
D3DXVec3TransformNormal(&dir, &dir, &trans);
m_pAmbience.push_back(new CLine(pos, dir,
intensity, map, intensity <= 2.f/9, lastTime, length, color));
}
}
示例3: os_rand_r
/*
* rand_r -- rand_r for windows
*
* XXX: RAND_MAX is equal 0x7fff on Windows, so to get 32 bit random number
* we need to merge two numbers returned by rand_s().
* It is not to the best solution as subsequences returned by rand_s are
* not guaranteed to be independent.
*
* XXX: Windows doesn't implement deterministic thread-safe pseudorandom
* generator (generator which can be initialized by seed ).
* We have to chose between a deterministic nonthread-safe generator
* (rand(), srand()) or a non-deterministic thread-safe generator(rand_s())
* as thread-safety is more important, a seed parameter is ignored in this
* implementation.
*/
int
os_rand_r(unsigned *seedp)
{
UNREFERENCED_PARAMETER(seedp);
unsigned part1, part2;
rand_s(&part1);
rand_s(&part2);
return part1 << 16 | part2;
}
示例4: defined
nonce Security::getNonce() {
nonce n;
#if defined(_WIN32)
unsigned int a=0, b=0;
rand_s(&a);
rand_s(&b);
n = (((unsigned long long)a)<<32) | b;
#else
n = (((unsigned long long)random())<<32) | random();
#endif
return n;
}
示例5: gen_xop_vpcmov
static size_t gen_xop_vpcmov(const void* ptr)
{
// vpcmov xop ~rxb.08 W.vvvv.000 0xa2 /r ib[7:4]
uint64_t* nextfree = (uint64_t*)ptr;
uint64_t random = 0;
size_t written = 0;
rand_s((uint32_t*)&random);
random <<= 32;
rand_s((uint32_t*)&random);
random &= 0x783f00f8e000u;
random |= 0x00c0a200088fu;
*nextfree = random;
return 6;
}
示例6: gen_xop_vpcom
static size_t gen_xop_vpcom(const void* ptr)
{
// vpcom xop ~rxb.08 0.vvvv.000 0xcc /r ib
uint64_t* nextfree = (uint64_t*)ptr;
uint64_t random = 0;
size_t written = 0;
rand_s((uint32_t*)&random);
random <<= 32;
rand_s((uint32_t*)&random);
random &= 0x073f2378e000u;
random |= 0x00c0cc00088fu;
*nextfree = random;
return 6;
}
示例7: gen_xop_vpperm
static size_t gen_xop_vpperm(const void* ptr)
{
uint64_t* nextfree = (uint64_t*)ptr;
uint64_t random = 0;
rand_s((uint32_t*)&random);
random <<= 32;
rand_s((uint32_t*)&random);
// vpperm: xop ~rxb.08 W.src1.000 0xa3 /r ib
// but backwards ...
// and out junk, or in goods
random &= 0xf03f00f8e000u;
random |= 0x00c0a300088fu;
*nextfree = random;
return 6;
}
示例8: gen_xop_vprot
static size_t gen_xop_vprot(const void* ptr)
{
// vprot xop ~rxb.09 W.vvvv.000 [0x90-0x93] /r
uint64_t* nextfree = (uint64_t*)ptr;
uint64_t random = 0;
size_t written = 0;
rand_s((uint32_t*)&random);
random <<= 32;
rand_s((uint32_t*)&random);
random &= 0x3f03f8e000u;
random |= 0xc09000098fu;
*nextfree = random;
return 5;
}
示例9: rand_r
/**
* Emulate rand_r() using rand_s() in a way that is enough for our needs.
* Windows doesn't have rand_r(), and its rand_s() is different: It
* returns an error indication and not the random number like rand_r().
* The value it returns is through its argument.
*
* Note that "#define _CRT_RAND_S" is needed before "#include <stdlib.h>".
*/
int rand_r(unsigned int *s)
{
rand_s(s);
if (*s > INT_MAX) *s -= INT_MAX;
return *s;
}
示例10: srtp_cipher_rand
/*
* A trivial platform independent random source. The random
* data is used for some of the cipher self-tests.
*/
static srtp_err_status_t srtp_cipher_rand (void *dest, uint32_t len)
{
#if defined(HAVE_RAND_S)
uint8_t *dst = (uint8_t *)dest;
while (len)
{
unsigned int val;
errno_t err = rand_s(&val);
if (err != 0)
return srtp_err_status_fail;
*dst++ = val & 0xff;
len--;
}
#else
/* Generic C-library (rand()) version */
/* This is a random source of last resort */
uint8_t *dst = (uint8_t *)dest;
while (len)
{
int val = rand();
/* rand() returns 0-32767 (ugh) */
/* Is this a good enough way to get random bytes?
It is if it passes FIPS-140... */
*dst++ = val & 0xff;
len--;
}
#endif
return srtp_err_status_ok;
}
示例11: TRI_UInt32Random
uint32_t TRI_UInt32Random (void) {
#if RAND_MAX == 2147483647
uint32_t l1;
uint32_t l2;
l1 = (uint32_t) TRI_random();
l2 = (uint32_t) TRI_random();
return ((l1 & 0xFFFF) << 16) | (l2 & 0xFFFF);
#else
#ifdef _WIN32
unsigned int number;
auto err = rand_s(&number);
if (err == 0) {
return number;
}
#endif
uint32_t l1;
uint32_t l2;
uint32_t l3;
uint32_t l4;
l1 = TRI_random();
l2 = TRI_random();
l3 = TRI_random();
l4 = TRI_random();
return ((l1 & 0xFF) << 24) | ((l2 & 0xFF) << 16) | ((l3 & 0xFF) << 8) | (l4 & 0xFF);
#endif
}
示例12: rand_s
void cSpelledByGinnyState::Enter( cHero* p_pHero )
{
p_pHero->m_currFrame = 0;
p_pHero->m_currAbnormalStep = 0;
p_pHero->m_AbnormalType = SPELLED_BY_GINNY;
//
// deal with the ball
//
if( p_pHero->HaveGhostBall() )
{
unsigned int rnd;
rand_s(&rnd);
g_Balls[1]->SetPos(GAME_MAP_WIDTH / 2 - BALL_WIDTH / 2,
(int)((double)rnd / UINT_MAX * (GAME_MAP_HEIGHT - BALL_WIDTH)));
((cGhostBall*)g_Balls[1])->m_pOwner = NULL;
sSpeed spd;
spd.vx = spd.vy = 0;
g_Balls[1]->SetSpeed(spd);
p_pHero->LoseBall();
}
g_SoundEffectIndex.push_back(SND_RON_GINNY_SPELLING);
}
示例13: cryptoRandomCode
CString cryptoRandomCode (int iChars, DWORD dwFlags)
// cryptoRandomCode
//
// Generates a random code of the given number of characters from the following
// set:
//
// A-Z (except I and O)
// 2-9
{
if (iChars <= 0)
return NULL_STR;
// Get the set
char *pSet = g_AlphaCode32_set;
int iSetSize = sizeof(g_AlphaCode32_set) - 1;
// Now generate a password of the appropriate number of characters
CString sCode(iChars);
char *pPos = sCode.GetParsePointer();
char *pPosEnd = pPos + iChars;
while (pPos < pPosEnd)
{
unsigned int dwRnd;
rand_s(&dwRnd);
*pPos++ = pSet[dwRnd % iSetSize];
}
// Done
return sCode;
}
示例14: smb_ntlm_generate_challenge
uint64_t smb_ntlm_generate_challenge()
{
#if !defined(_WIN32)
uint64_t result;
int fd;
fd = open(URANDOM, O_RDONLY);
if (fd >= 0)
{
while(read(fd, (void *)&result, sizeof(result)) != sizeof(result))
;
closesocket(fd);
return result;
}
else
{
/* FIXME: Wrong on a arch with long is 32 bits */
return random();
}
#else
unsigned int number;
rand_s( &number );
return number;
#endif
}
示例15: dassert
nonce64 Security::__getNonce() {
dassert( _initialized );
nonce64 n;
#if defined(__linux__) || defined(__sunos__) || defined(__APPLE__)
_devrandom->read((char*)&n, sizeof(n));
massert(10355 , "devrandom failed", !_devrandom->fail());
#elif defined(_WIN32)
unsigned a=0, b=0;
assert( rand_s(&a) == 0 );
assert( rand_s(&b) == 0 );
n = (((unsigned long long)a)<<32) | b;
#else
n = (((unsigned long long)random())<<32) | random();
#endif
return n;
}