本文整理汇总了C++中RND函数的典型用法代码示例。如果您正苦于以下问题:C++ RND函数的具体用法?C++ RND怎么用?C++ RND使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RND函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: HomedGhost
static void HomedGhost(GAME_STATE *ptr, G_GHOST *pGhost)
{
char c;
switch(pGhost->iInHome)
{
case GIH_SHUFFLE:
/* Move up into gateway?? */
if (RND(2) == 0 && Pac_GetMap(ptr,pGhost->Pos.x,pGhost->Pos.y-1,&c) && c=='-')
{
pGhost->iInHome = GIH_GATEWAY;
pGhost->Pos.y--;
}
else
{
c = RND(3);
if (c == 0 && Pac_GetMap(ptr,pGhost->Pos.x-1,pGhost->Pos.y,&c) && c=='H')
pGhost->Pos.x--;
else if (c == 1 && Pac_GetMap(ptr,pGhost->Pos.x+1,pGhost->Pos.y,&c) && c=='H')
pGhost->Pos.x++;
}
break;
case GIH_GATEWAY:
pGhost->Pos.y--; /* ASSUME: gate opens upwards */
pGhost->iInHome = GIH_OUTSIDE;
if (RND(2) == 0)
pGhost->Direction = eDIR_Left;
else
pGhost->Direction = eDIR_Right;
break;
}
}
示例2: rc6_ecb_encrypt
int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
#endif
{
ulong32 a,b,c,d,t,u, *K;
int r;
LTC_ARGCHK(skey != NULL);
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]);
b += skey->rc6.K[0];
d += skey->rc6.K[1];
#define RND(a,b,c,d) \
t = (b * (b + b + 1)); t = ROLc(t, 5); \
u = (d * (d + d + 1)); u = ROLc(u, 5); \
a = ROL(a^t,u) + K[0]; \
c = ROL(c^u,t) + K[1]; K += 2;
K = skey->rc6.K + 2;
for (r = 0; r < 20; r += 4) {
RND(a,b,c,d);
RND(b,c,d,a);
RND(c,d,a,b);
RND(d,a,b,c);
}
#undef RND
a += skey->rc6.K[42];
c += skey->rc6.K[43];
STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
return CRYPT_OK;
}
示例3: rc6_ecb_decrypt
void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
#endif
{
ulong32 a,b,c,d,t,u, *K;
int r;
LTC_ARGCHK(skey != NULL);
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]);
a -= skey->rc6.K[42];
c -= skey->rc6.K[43];
#define RND(a,b,c,d) \
t = (b * (b + b + 1)); t = ROLc(t, 5); \
u = (d * (d + d + 1)); u = ROLc(u, 5); \
c = ROR(c - K[1], t) ^ u; \
a = ROR(a - K[0], u) ^ t; K -= 2;
K = skey->rc6.K + 40;
for (r = 0; r < 20; r += 4) {
RND(d,a,b,c);
RND(c,d,a,b);
RND(b,c,d,a);
RND(a,b,c,d);
}
#undef RND
b -= skey->rc6.K[0];
d -= skey->rc6.K[1];
STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
}
示例4: MoveGhost
static void MoveGhost(GAME_STATE *ptr, G_GHOST *pGhost)
{
int tries=0;
if (!IsNextMoveValid(ptr, pGhost))
{ /* must be either 1 or 2 valid directions */
tDir w1,w2;
switch(pGhost->Direction)
{
case eDIR_Right:
case eDIR_Left: w1=eDIR_Up, w2=eDIR_Down; break;
case eDIR_Up:
case eDIR_Down: w1=eDIR_Left, w2=eDIR_Right; break;
}
if (RND(2)==0) /* try w1 first, then w2 */
{
pGhost->Direction = w1;
if (!IsNextMoveValid(ptr, pGhost))
pGhost->Direction = w2;
}
else
{
pGhost->Direction = w2;
if (!IsNextMoveValid(ptr, pGhost))
pGhost->Direction = w1;
}
}
if (RND(34) == 0 && pGhost->Pos.x != ptr->Player.Pos.x && pGhost->Pos.y != ptr->Player.Pos.y)
{
pGhost->Direction = RND(4); /* a little bit bad! */
while(++tries < 4)
{
if (IsNextMoveValid(ptr, pGhost))
break;/* found a new dir */
else
if (++pGhost->Direction == 4)
pGhost->Direction = 0;
}
}
/* Move the ghost */
switch(pGhost->Direction)
{
case eDIR_Left: pGhost->Pos.x--; break;
case eDIR_Right: pGhost->Pos.x++; break;
case eDIR_Up: pGhost->Pos.y--; break;
case eDIR_Down: pGhost->Pos.y++; break;
}
/* Check for tunnel */
if (pGhost->Pos.x < 0) pGhost->Pos.x = ptr->iMapWidth-1;
if (pGhost->Pos.x >= ptr->iMapWidth) pGhost->Pos.x = 0;
}
示例5: calc_entropy
void calc_entropy (
MODEL *model, /* the model */
DATASET *dataset /* the dataset */
)
{
int i, j;
double *rentropy = model->rentropy; /* IC of each column */
double ent = 0; /* entropy per column */
double rel = 0; /* relative entropy per col. */
int alength = dataset->alength; /* length of alphabet */
double *back = dataset->back; /* background model freqs */
THETA obs = model->obs; /* observed frequencies */
int w = model->w; /* width of motif */
int N = model->nsites_dis; /* number of sites */
double log_pop; /* log product of col p-value */
double max_ic = LOG(alength)/LOG(2); // maximum IC per column
//double e = (alength-1) / (2 * LOG(2) * N); // small sample correction
double ic = 0; // "corrected" IC
/* calculate the relative entropy of each column in motif */
model->llr = log_pop = 0;
for (i=0; i<w; i++) { /* position */
double llr; /* log likelihood ratio of column */
rentropy[i] = 0.0;
double H = 0; // negative entropy in this column
for (j=0; j<alength; j++) { /* alphabet letter */
double f = obs(i, j); /* motif freq */
double p = back[j]; /* background freq */
double h = f ? f * LOG2(f) : 0; // entropy of current letter
rel += p ? f * LOG2(p) : 0; /* total relative entropy */
ent += h; /* total entropy */
H += -h; // negative entropy in this column
rentropy[i] += (f && p) ? f * LOG(f/p) : 0;
} /* alphabet letter */
llr = N * rentropy[i]; /* log likelihood ratio */
RND(llr, RNDDIG, llr); /* round to RNDDIG places */
model->llr += llr; /* llr for model */
log_pop += get_llr_pv(llr, N, 1, LLR_RANGE, 1.0, alength, back);
rentropy[i] /= LOG(2);
// ic += MAX(0, (max_ic - (H + e)));
ic += max_ic - H;
} /* position in motif */
/* compute the log E-value of the motif */
model->logev = get_log_sig(-log_pop, model->mtype, w, N, N, model->invcomp,
model->pal, dataset);
model->rel = (ent - rel)/w; /* compute rel. entropy/col */
// LOGO total information content
RND(ic, RNDDIG, ic); // round to RNDDIG places
model->ic = ic;
} /* calc_entropy */
示例6: are
/* This a helper function that generates a random password with a
number of characters from a set of character classes.
If there are n classes, and the size of each class is Pi, and the
number of characters from each class is Ni, the number of possible
passwords are (given that the character classes are disjoint):
n n
----- / ---- \
| | Ni | \ |
| | Pi | \ Ni| !
| | ---- * | / |
| | Ni! | /___ |
i=1 \ i=1 /
Since it uses the RND function above, neither the size of each
class, nor the total length of the generated password should be
larger than 127 (without fixing RND).
*/
static void
generate_password(char **pw, int num_classes, ...)
{
struct {
const char *str;
int len;
int freq;
} *classes;
va_list ap;
int len, i;
unsigned char rbuf[8]; /* random buffer */
int rleft = 0;
*pw = NULL;
classes = malloc(num_classes * sizeof(*classes));
if(classes == NULL)
return;
va_start(ap, num_classes);
len = 0;
for(i = 0; i < num_classes; i++) {
classes[i].str = va_arg(ap, const char*);
classes[i].len = strlen(classes[i].str);
classes[i].freq = va_arg(ap, int);
len += classes[i].freq;
}
va_end(ap);
*pw = malloc(len + 1);
if(*pw == NULL) {
free(classes);
return;
}
for(i = 0; i < len; i++) {
int j;
int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i);
int t = 0;
for(j = 0; j < num_classes; j++) {
if(x < t + classes[j].freq) {
(*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft)
% classes[j].len];
classes[j].freq--;
break;
}
t += classes[j].freq;
}
}
(*pw)[len] = '\0';
memset(rbuf, 0, sizeof(rbuf));
free(classes);
}
示例7: Transform
static void Transform(Sha256* sha256)
{
word32 S[8], W[64], t0, t1;
int i;
/* Copy context->state[] to working vars */
for (i = 0; i < 8; i++)
S[i] = sha256->digest[i];
for (i = 0; i < 16; i++)
W[i] = sha256->buffer[i];
for (i = 16; i < 64; i++)
W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
for (i = 0; i < 64; i += 8) {
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
}
/* Add the working vars back into digest state[] */
for (i = 0; i < 8; i++) {
sha256->digest[i] += S[i];
}
}
示例8: sha512_compress
void sha512_compress(psDigestContext_t * md, unsigned char *buf)
#endif
{
uint64 S[8], W[80], t0, t1;
int i;
/* copy state into S */
for (i = 0; i < 8; i++) {
S[i] = md->sha512.state[i];
}
/* copy the state into 1024-bits into W[0..15] */
for (i = 0; i < 16; i++) {
LOAD64H(W[i], buf + (8*i));
}
/* fill W[16..79] */
for (i = 16; i < 80; i++) {
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
}
/* Compress */
#ifndef PS_SHA512_IMPROVE_PERF_INCREASE_CODESIZE
for (i = 0; i < 80; i++) {
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
S[7] = S[6];
S[6] = S[5];
S[5] = S[4];
S[4] = S[3] + t0;
S[3] = S[2];
S[2] = S[1];
S[1] = S[0];
S[0] = t0 + t1;
}
#else
#define RND(a,b,c,d,e,f,g,h,i) \
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
t1 = Sigma0(a) + Maj(a, b, c); \
d += t0; \
h = t0 + t1;
for (i = 0; i < 80; i += 8) {
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
}
#endif /* PS_SHA512_IMPROVE_PERF_INCREASE_CODESIZE */
/* feedback */
for (i = 0; i < 8; i++) {
md->sha512.state[i] = md->sha512.state[i] + S[i];
}
}
示例9: tryInsert
int tryInsert()
{
double testx, testy;
testx=RND()*L;
testy=RND()*L;
if (Pinsert(testx, testy))
{
X[n]=testx;
Y[n]=testy;
printf("%d: inserted at %lf, %lf\n",n, testx,testy);
n++;
return 1;
}
else return 0;
}
示例10: memcpy
static Header *compact(MemBlk *mb)
{
char *end = (char *) (mb+1) + mb->size;
Header *h = (Header *) (mb+1);
Header *to, *pto=NULL;
for (to=h ; (char *) h < end ; h = NEXTH(h)) {
if (h->status == 'F')
continue;
if (to != h) {
memcpy(to, h, h->rsize+sizeof(Header));
if (h->next)
*((Header **) to->next) = to + 1;
}
to->size = RND(to->rsize);
pto = to;
to = NEXTH(to);
}
if ((char *) to >= end)
return NULL;
to->status = 'F';
to->size = (end - (char *) to) - sizeof(Header);
if (!to->size) {
if (pto)
pto->size += to->size + sizeof(Header);
return NULL;
}
return to;
}
示例11: main
int main(){
struct cache_manager *lru_manager;
int i;
lru_init(&lru_manager,"LRU", 500, 500, 1, 0);
for(i =0;i < 1000000;i++){
struct lru_node *ln = NULL;
unsigned int blkno = RND(10000);
ln = CACHE_SEARCH(lru_manager, blkno);
if(!ln){
ln = CACHE_REPLACE(lru_manager, 0, FCL_REPLACE_ANY);
ln = CACHE_ALLOC(lru_manager, ln, blkno);
CACHE_INSERT(lru_manager, ln);
}else{
ln = CACHE_REMOVE(lru_manager, ln);
CACHE_INSERT(lru_manager, ln);
}
}
CACHE_PRINT(lru_manager, stdout);
CACHE_CLOSE(lru_manager);
return 0;
}
示例12: sha256_compress
/* compress 512-bits */
static int sha256_compress(struct sha256_state *md,
unsigned char *buf)
{
unsigned long S[8], W[64], t0, t1;
unsigned long t;
int i;
/* copy state into S */
for(i = 0; i < 8; i++) {
S[i] = md->state[i];
}
/* copy the state into 512-bits into W[0..15] */
for(i = 0; i < 16; i++)
W[i] = WPA_GET_BE32(buf + (4 * i));
/* fill W[16..63] */
for(i = 16; i < 64; i++) {
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
W[i - 16];
}
/* Compress */
#define RND(a,b,c,d,e,f,g,h,i) \
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
t1 = Sigma0(a) + Maj(a, b, c); \
d += t0; \
h = t0 + t1;
for(i = 0; i < 64; ++i) {
RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
}
/* feedback */
for(i = 0; i < 8; i++) {
md->state[i] = md->state[i] + S[i];
}
return 0;
}
示例13: more_core
static void more_core(unsigned n)
{
unsigned sz;
unsigned as;
MemBlk *mb;
Header *h;
sz = n > DBS ? n : DBS;
sz = RND(sz);
as = sizeof(MemBlk) + sizeof(Header) + sz;
mb = (MemBlk *) malloc(as);
if (!mb) {
#ifndef __COSMIC__
fprintf(stderr, "\nOut of memory.\n");
#endif
#line 235 "memalloc.d"
exit(1);
}
mb->next = MMBP;
MMBP = mb;
mb->size = sz + sizeof(Header);
h = (Header *) (mb+1);
h->status = 'U';
h->next = NULL;
h->size = sz;
MA_free(h+1);
}
示例14: DormantGhost
static void DormantGhost(GAME_STATE *ptr, G_GHOST *pGhost)
{
/* Wake him up?? */
if (RND(30) < 2)
Pac_ActivateGhost(pGhost, 12,10,eDIR_Left);
}
示例15: d
int d(int n, int x)
{
int tmp = n;
while (n--)
tmp += RND(x);
return tmp;
}