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); }
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)); } }
/* * 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; }
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; }
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; }
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; }
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; }
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; }
/** * 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; }
/* * 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; }
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 }
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); }
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; }
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 }
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; }
int32_t PseudoRandom::nextInt32() { if ( rand_s(&_seed) == 0 ) { // SUCCESS return _seed; } abort(); }
void CTetrisDlg::NextRandomBlock() { UINT r; rand_s(&r); m_gameParam &= 0x1F; m_gameParam |= ((BYTE)((DOUBLE)r / ((__int64)UINT_MAX + 1) * 7) + 1) << 5; m_nextColor = NextRandomColor(); }
uint32_t pal_rand32 () { unsigned int i_rand_number = 0; errno_t i_rand_ret = -1; i_rand_ret = rand_s(&i_rand_number); return (uint32_t) i_rand_number; }
void fill_random(uint8_t* buffer, int len) { unsigned int number; int i = 0; for (i=0; i < len; ++i) { rand_s(&number); buffer[i] = (number%256); } }
unsigned random_device::operator()() { unsigned r; errno_t err = rand_s(&r); if (err) __throw_system_error(err, "random_device rand_s failed."); return r; }
void SessionInfo::GenerateClientSessionID() { unsigned char bytes[CLIENT_SESSION_ID_BYTES]; assert(CLIENT_SESSION_ID_BYTES % sizeof(unsigned int) == 0); for (int i=0; i < CLIENT_SESSION_ID_BYTES/sizeof(unsigned int); i++) { rand_s(((unsigned int*)bytes) + i); } m_clientSessionID = Hexlify(bytes, CLIENT_SESSION_ID_BYTES); }
unsigned int random_gen() { unsigned int rnd = 0; #ifdef WIN32 rand_s(&rnd); #else rnd = rand(); #endif return rnd; }
unsigned long Encryption::GetRand (int iMax) { errno_t err; double max = iMax; unsigned int number; err = rand_s(&number); return (double) number / (double) UINT_MAX * max; }
void gen_tmpname(char* const buf, const size_t n) { if (n < 1) return; buf[n -1] = 0x00; if (n < 2) return; buf[0] = '.'; rand_s(buf+1, n-2); }
inline double urandom() { unsigned int number; errno_t err; err = rand_s(&number); if (err!=0) { printf("something wrong\n"); return -1; } return (double)number / (double)UINT_MAX; }
void PacketSerialize(long nPackets, CArchive& arData, CArchive& arAck) { BYTE bValue = 0; WORD nCopies = 0; if (arData.IsStoring()) { CString strText; errno_t err; unsigned int number; for(int p = 0; p < nPackets; p++) { err = rand_s(&number); // if (err == 0)... bValue = (BYTE)(number%256); err = rand_s(&number); // if (err == 0)... nCopies = (WORD)(number%32000); // Send header information arData << bValue << nCopies; for(int c = 0; c < nCopies; c++) { // Send data arData << bValue; } strText.Format(_T("Sender sent packet %d of %d (Value = %d, Copies = %d)"), p + 1, nPackets, (int)bValue, nCopies); // Send receipt string arData << strText; arData.Flush(); // Receive acknowledgment arAck >> strText; // display it DisplayMessage(strText); } }
/*! \brief Generate a random number between 0 <= y_drand() < 1. This supersedes y_rand(). \n Equal to Math.random in Java and JavaScript, Random.NextDouble in C#, etc. Better distribution of the random numbers over the range than by using y_rand() with modulo (%) -- thus no skewed results. \return Random number between 0 and 1 (exclusive). Accuracy: 9.4 digits (32 bits) thus increments of approx. 2.3e-10. \b Examples: \code if (y_drand() < 0.123) ... -> change 12.3% true double value = min + y_drand() * (max - min); // to range example int value = min + y_drand() * (max - min + 1); // for min <= value <= max \endcode \author A.U. Luyer */ double y_drand(void) { #ifdef WINNT // On Windows we can use rand_s which returns a truly 32 bit random number. unsigned int randomValue; rand_s(&randomValue); return (double)randomValue / 4294967296.; #else return (rand() << 15 | rand())/1073741824.; #endif }
uint8_t RTPRandomRandS::GetRandom8() { if (!initialized) return 0; unsigned int r; rand_s(&r); return (uint8_t)(r&0xff); }
MOA_USE_RESULT static uint32_t rand_s_wrapper(void *state) { (void)state; unsigned int result; if (rand_s(&result) != 0) { abort(); } return result; }
unsigned int RandomNumberGenerator::generate () { static unsigned int val = 0; #ifdef WIN32 rand_s (&val); #else val = rand (); #endif return val; }