本文整理汇总了C++中RANDOM函数的典型用法代码示例。如果您正苦于以下问题:C++ RANDOM函数的具体用法?C++ RANDOM怎么用?C++ RANDOM使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RANDOM函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: naive_grasp
int
naive_grasp(data *d, int flags)
{
int dim = d->numnodes;
int start, cost, swaps;
int *tour, *best_tour, best_cost, no_improvements;
int use_three_opt = flags & DO_3OPT;
long int i, limit;
bool break_line = false;
tour = MALLOC(dim, int);
best_tour = MALLOC(dim, int);
limit = (long int)dim * dim;
printf("\nGRASP, %ld runs starting.. \n", limit);
best_cost = INT_MAX;
no_improvements = 0;
for (i = 0; i < limit; i++) {
start = RANDOM(dim);
semi_greedy(d, start, tour, &cost);
if (use_three_opt) {
local_3opt(d, tour, &cost, &swaps);
} else {
local_2opt(d, tour, &cost, &swaps);
}
if (cost < best_cost) {
if (flags & BE_VERBOSE) {
if (break_line) {
break_line = false;
printf("\n");
}
printf("%ld: %d, %d\n", i, cost, start);
}
best_cost = cost;
memcpy(best_tour, tour, dim * sizeof(int));
no_improvements = 0;
} else {
no_improvements++;
if (flags & BE_VERBOSE && !(no_improvements % 50)) {
printf(">");
break_line = true;
fflush(stdout);
}
}
if (no_improvements == 5000) {
if (flags & BE_VERBOSE) {
if (break_line) {
printf("\n");
break_line = false;
}
printf("No improvements for %d runs, stopping.\n",
no_improvements);
}
break;
}
}
if (flags & BE_VERBOSE) {
if (break_line) printf("\n");
show_sol(best_tour, dim, best_cost);
printf("Executions: %ld\n", i);
}
printf("\nGRASP final cost: %d\n", best_cost);
free(tour);
free(best_tour);
return 0;
}
开发者ID:gpolo,项目名称:STSP,代码行数:67,代码来源:naive_grasp.c
示例2: main
main(int argc, char *argv[]){
int i,j,k;
int nr, n_run;
int n,m;
double **A, **At, **G, **Gt, **GtG, **U, **Ut, **UtU, *w, **V, norm;
double *v;
FILE *stream;
if (argc <4){
fprintf(stderr,"Usage: %s m n n_run <datfile>\n", argv[0]);
exit(EXIT_FAILURE);
}
m = atoi(argv[1]);
n = atoi(argv[2]);
n_run = atoi(argv[3]);
if (argc == 5){
if ((stream = fopen(argv[4], "r")) == NULL){
fprintf(stderr, "Can't open file %s.\n", argv[4]);
exit(EXIT_FAILURE);
}
} else {
stream = stdin;
}
A = ALLOC_MATRIX(m,n);
/* for(i=0; i<m; ++i){ */
/* for(j=0; j<n; ++j){ */
/* if (fscanf(stream,"%lf", A[i]+j) != 1){ */
/* fprintf(stderr,"Error occured in reading the data.\n"); */
/* exit(EXIT_FAILURE); */
/* } */
/* } */
/* } */
for(i=0; i<m; ++i){
for(j=0; j<n; ++j){
A[i][j] = RANDOM(-10.0,10.0);
}
}
/* print A */
U = ALLOC_MATRIX(m,n);
COPY_VECTOR(A[0], U[0], m*n);
for(i=0; i<m; ++i){
for(j=0; j<n; ++j)
printf("%8.2f ", U[i][j]);
printf("\n");
}
printf("Using Gram-Schmidt\n");
/* gram-schmidt */
At = RECT_TRANSPOSE(A, m,n);
Gt = ALLOC_MATRIX(n,m);
for(i=0; i<n_run; ++i){
COPY_VECTOR(At[0], Gt[0], n*m);
GRAM_SCHMIDT(Gt,n,m);
}
printf("Orthogonal vectors are \n");
for (i=0; i<n; ++i){
for(j=0, norm=0.0; j<m; ++j){
norm += SQR(Gt[i][j]);
printf("%.6f ", Gt[i][j]);
}
printf(" %f\n",norm);
}
G = RECT_TRANSPOSE(Gt,n,m);
GtG = ALLOC_MATRIX(n,n);
printf("S=\n");
for(i=0; i<n; ++i){
for(j=0; j<n; ++j){
GtG[i][j]=0.0;
for(k=0; k<m; ++k)
GtG[i][j]+=Gt[i][k]*G[k][j];
printf("%8.1e ", GtG[i][j]);
}
printf("\n");
}
/* project random vector against orthogonal basis */
v = ALLOC_VECTOR(m);
for(i=0; i<m; ++i)
v[i] = RANDOM(-10,10);
PROJECT(v, m, Gt, n);
for(i=0; i<n; ++i)
printf("v.G[%2d] = %9.2e\n", i, DOTP(v,Gt[i],m));
}
开发者ID:SysSynBio,项目名称:bionetgen,代码行数:88,代码来源:testgram.c
示例3: memory_case
/* benchmark alloc / free */
ilong memory_case(ilong limit, ilong hiwater, ilong times, int rate, ilong seed)
{
struct case_t { ilong size, m1, m2; char *ptr; };
struct case_t *record, *p;
iulong startup = 0, water = 0, maxmem = 0, sizev = 0;
ilong pagesize, page_in, page_out, page_inuse;
double waste;
char *ptr;
int count = 0, maxium = 0;
int mode, size, pos;
int retval = 0;
record = (struct case_t*)malloc(100);
xseed = seed;
startup = gettime();
for (; times >= 0; times--) {
mode = 0;
if (RANDOM(100) < rate) {
size = RANDOM(limit);
if (size < sizeof(ilong) * 2) size = sizeof(ilong) * 2;
if (water + size >= hiwater) mode = 1;
} else {
mode = 1;
}
/* TO ALLOC new memory block */
if (mode == 0) {
if (count + 4 >= maxium) {
maxium = maxium? maxium * 2 : 8;
maxium = maxium >= count + 4? maxium : count + 4;
sizev = maxium * sizeof(struct case_t);
record = (struct case_t*)realloc(record,
maxium * sizeof(struct case_t));
assert(record);
}
ptr = xmalloc(size);
assert(ptr);
/* record pointer */
p = &record[count++];
p->ptr = ptr;
p->size = size;
p->m1 = rand() & 0x7ffffff;
p->m2 = rand() & 0x7ffffff;
water += size;
/* writing magic data */
*(ilong*)ptr = p->m1;
*(ilong*)(ptr + p->size - sizeof(ilong)) = p->m2;
}
/* TO FREE old memory block */
else if (count > 0) {
pos = RANDOM(count);
record[count] = record[pos];
p = &record[count];
record[pos] = record[--count];
ptr = p->ptr;
/* checking magic data */
if (*(ilong*)ptr != p->m1) {
printf("[BAD] bad magic1: %lxh size=%d times=%d\n", ptr, p->size, times);
return -1;
}
if (*(ilong*)(ptr + p->size - sizeof(ilong)) != p->m2) {
printf("[BAD] bad magic2: %lxh size=%d times=%d\n", ptr, p->size, times);
return -1;
}
xfree(ptr);
water -= p->size;
}
if (water > maxmem) maxmem = water;
}
/* state page-supplier */
page_in = imem_gfp_default.pages_new;
page_out = imem_gfp_default.pages_del;
page_inuse = imem_gfp_default.pages_inuse;
pagesize = imem_page_size;
/* free last memory blocks */
for (pos = 0; pos < count; pos++) {
p = &record[pos];
ptr = p->ptr;
if (*(ilong*)ptr != p->m1) {
printf("[BAD] bad magic: %lxh\n", ptr);
return -1;
}
if (*(ilong*)(ptr + p->size - sizeof(ilong)) != p->m2) {
printf("[BAD] bad magic: %lxh\n", ptr);
return -1;
}
xfree(ptr);
}
/* caculate time */
startup = gettime() - startup;
free(record);
if (kmem_turnon && water > 0) {
waste = (pagesize * page_inuse - water) * 100.0 / water;
} else {
waste = 0;
//.........这里部分代码省略.........
开发者ID:Bad-ptr,项目名称:memslab,代码行数:101,代码来源:testbench.c
示例4: effect_tick
void effect_tick(void)
{
int n,cnt=0,in,m,co,fn,cn,in2,flag,z;
for (n=1; n<MAXEFFECT; n++) {
if (fx[n].used==USE_EMPTY) continue;
cnt++;
if (fx[n].used!=USE_ACTIVE) continue;
if (fx[n].type==1) { // remove injury flag from map
fx[n].duration--;
if (fx[n].duration==0) {
fx[n].used=USE_EMPTY;
map[fx[n].data[0]+fx[n].data[1]*MAPX].flags&=~(MF_GFX_INJURED|MF_GFX_INJURED1|MF_GFX_INJURED2);
}
}
if (fx[n].type==2) { // timer for character respawn
if (fx[n].duration) fx[n].duration--;
if (fx[n].duration==0 && plr_check_target(fx[n].data[0]+fx[n].data[1]*MAPX)) {
m=fx[n].data[0]+fx[n].data[1]*MAPX;
map[m].flags|=MF_MOVEBLOCK;
fx[n].type=8;
}
}
if (fx[n].type==3) { // death mist
fx[n].duration++;
if (fx[n].duration==19) {
fx[n].used=0;
m=fx[n].data[0]+fx[n].data[1]*MAPX;
map[m].flags&=~MF_GFX_DEATH;
} else {
m=fx[n].data[0]+fx[n].data[1]*MAPX;
map[m].flags&=~MF_GFX_DEATH;
map[m].flags|=((unsigned long long)fx[n].duration)<<40;
if (fx[n].duration==9) {
plr_map_remove(fx[n].data[2]);
if (can_drop(m)) ;
else if (can_drop(m+1)) m+=1;
else if (can_drop(m-1)) m+=-1;
else if (can_drop(m+MAPX)) m+=MAPX;
else if (can_drop(m-MAPX)) m+=-MAPX;
else if (can_drop(m+1+MAPX)) m+=1+MAPX;
else if (can_drop(m+1-MAPX)) m+=1-MAPX;
else if (can_drop(m-1+MAPX)) m+=-1+MAPX;
else if (can_drop(m-1-MAPX)) m+=-1-MAPX;
else if (can_drop(m+2)) m+=2;
else if (can_drop(m-2)) m+=-2;
else if (can_drop(m+2*MAPX)) m+=2*MAPX;
else if (can_drop(m-2*MAPX)) m+=-2*MAPX;
else if (can_drop(m+2+MAPX)) m+=2+MAPX;
else if (can_drop(m+2-MAPX)) m+=2-MAPX;
else if (can_drop(m-2+MAPX)) m+=-2+MAPX;
else if (can_drop(m-2-MAPX)) m+=-2-MAPX;
else if (can_drop(m+1+2*MAPX)) m+=1+2*MAPX;
else if (can_drop(m+1-2*MAPX)) m+=1-2*MAPX;
else if (can_drop(m-1+2*MAPX)) m+=-1+2*MAPX;
else if (can_drop(m-1-2*MAPX)) m+=-1-2*MAPX;
else if (can_drop(m+2+2*MAPX)) m+=2+2*MAPX;
else if (can_drop(m+2-2*MAPX)) m+=2-2*MAPX;
else if (can_drop(m-2+2*MAPX)) m+=-2+2*MAPX;
else if (can_drop(m-2-2*MAPX)) m+=-2-2*MAPX;
else {
int temp;
co=fx[n].data[2];
temp=ch[co].temp;
chlog(co,"could not drop grave");
god_destroy_items(co);
ch[co].used=USE_EMPTY;
if (ch[co].flags&CF_RESPAWN)
fx_add_effect(2,TICKS*60*5+RANDOM(TICKS*60*10),ch_temp[temp].x,ch_temp[temp].y,temp);
m=0;
}
if (m) {
co=fx[n].data[2];
flag=0;
for (z=0; z<40 && !flag; z++) {
if (ch[co].item[z]) { flag=1; break; }
}
for (z=0; z<20 && !flag; z++) {
if (ch[co].worn[z]) { flag=1; break; }
}
if (ch[co].citem) flag=1;
if (ch[co].gold) flag=1;
if (flag) {
map[m].flags|=MF_MOVEBLOCK;
fn=fx_add_effect(4,0,m%MAPX,m/MAPX,fx[n].data[2]);
fx[fn].data[3]=fx[n].data[3];
//.........这里部分代码省略.........
开发者ID:AstoniaDev,项目名称:openMerc,代码行数:101,代码来源:svr_effect.c
示例5: RenderEntityCreateQuadTextured
TGarden *GardenCreate(const float pWidth, const float pHeight, const float pX, const float pY, const float pZ)
{
/*! Create Ground Plane */
TRenderEntity *lPlane = RenderEntityCreateQuadTextured(pWidth, pHeight, GARDEN_GROUND_TEXTURE);
if(lPlane == NULL)
return NULL;
CLASS_CREATE(TGarden);
Matrix4GetIdentity(&this->mModelView);
Matrix4SetTranslate(&this->mModelView, pX, pY, pZ);
this->mGround[0] = RenderEntityClone(lPlane);
RenderEntityRotate(this->mGround[0], 90.0f, 1.0f, 0.0f, 0.0f);
this->mGround[1] = RenderEntityClone(lPlane);
RenderEntityRotate(this->mGround[1],-90.0f, 1.0f, 0.0f, 0.0f);
RenderEntityDestroy(lPlane);
/*! Dummy Grass which will be cloned as necessary */
TGrass *lGrass = GrassCreate(0.0f, 0.0f, 0.0f, 0, GRASS_SIN_WAVE);
if(lGrass!=NULL)
{
int i,j;
vec2 lOffsets[GARDEN_MAX_GRASS_RINGS] =
{
{0.0f, 0.0f},
{0.0f, 0.0f},
{0.0f, 0.0f}
};
for(j=0;j<GARDEN_MAX_GRASS_RINGS;j++)
for(i=0;i<GARDEN_MAX_GRASS_LINES;i++)
{
TGrass *lClone = GrassClone(lGrass);
float lAngle = i * (2.0*M_PI/GARDEN_MAX_GRASS_LINES);
float lX = cos(lAngle)*(15.0f - (j*5));
float lZ = sin(lAngle)*(15.0f - (j*5));
GrassSetPosition(lClone, lX + lOffsets[j][0], 0.0f, lZ + lOffsets[j][1]);
//! Random Type
GrassSetType(lClone, RANDOM(GRASS_MAX_TYPES));
//! Random Number of Segments
GrassSetNumSegments(lClone, RANDOM(GRASS_MAX_SEGMENTS)+16);
//! Inner Ring is Red
if(j == GARDEN_MAX_GRASS_RINGS-1)
{
GrassSetColor(lClone, 0.8f, 0.0f, 0.0f, 1.0f);
}
this->mGrass[j][i] = lClone;
}
GrassDestroy(lGrass);
}
else
{
printf("Garden: grass couldn't be created for some reason. Crash is ahead!\n");
}
CLASS_INSTANCE();
}
开发者ID:icebreaker,项目名称:LDPrototype,代码行数:68,代码来源:garden.c
示例6: main
int
main (int argc, char *argv[])
{
gl_list_t list1, list2, list3;
set_program_name (argv[0]);
/* Allow the user to provide a non-default random seed on the command line. */
if (argc > 1)
srand (atoi (argv[1]));
{
size_t initial_size = RANDOM (50);
const void **contents =
(const void **) malloc (initial_size * sizeof (const void *));
size_t i;
unsigned int repeat;
for (i = 0; i < initial_size; i++)
contents[i] = RANDOM_OBJECT ();
/* Create list1. */
list1 = gl_list_nx_create (GL_ARRAY_LIST, NULL, NULL, NULL, true,
initial_size, contents);
ASSERT (list1 != NULL);
/* Create list2. */
list2 = gl_list_nx_create_empty (GL_RBTREE_LIST, NULL, NULL, NULL, true);
ASSERT (list2 != NULL);
for (i = 0; i < initial_size; i++)
ASSERT (gl_list_nx_add_last (list2, contents[i]) != NULL);
/* Create list3. */
list3 = gl_list_nx_create (GL_RBTREE_LIST, NULL, NULL, NULL, true,
initial_size, contents);
ASSERT (list3 != NULL);
check_all (list1, list2, list3);
for (repeat = 0; repeat < 10000; repeat++)
{
unsigned int operation = RANDOM (16);
switch (operation)
{
case 0:
if (gl_list_size (list1) > 0)
{
size_t index = RANDOM (gl_list_size (list1));
const char *obj = RANDOM_OBJECT ();
gl_list_node_t node1, node2, node3;
node1 = gl_list_nx_set_at (list1, index, obj);
ASSERT (node1 != NULL);
ASSERT (gl_list_get_at (list1, index) == obj);
ASSERT (gl_list_node_value (list1, node1) == obj);
node2 = gl_list_nx_set_at (list2, index, obj);
ASSERT (node2 != NULL);
ASSERT (gl_list_get_at (list2, index) == obj);
ASSERT (gl_list_node_value (list2, node2) == obj);
node3 = gl_list_nx_set_at (list3, index, obj);
ASSERT (node3 != NULL);
ASSERT (gl_list_get_at (list3, index) == obj);
ASSERT (gl_list_node_value (list3, node3) == obj);
if (index > 0)
{
ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1))
== gl_list_get_at (list1, index - 1));
ASSERT (gl_list_node_value (list2, gl_list_previous_node (list3, node3))
== gl_list_get_at (list2, index - 1));
ASSERT (gl_list_node_value (list3, gl_list_previous_node (list3, node3))
== gl_list_get_at (list2, index - 1));
}
if (index + 1 < gl_list_size (list1))
{
ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1))
== gl_list_get_at (list1, index + 1));
ASSERT (gl_list_node_value (list2, gl_list_next_node (list3, node3))
== gl_list_get_at (list2, index + 1));
ASSERT (gl_list_node_value (list3, gl_list_next_node (list3, node3))
== gl_list_get_at (list2, index + 1));
}
}
break;
case 1:
{
const char *obj = RANDOM_OBJECT ();
gl_list_node_t node1, node2, node3;
node1 = gl_list_search (list1, obj);
node2 = gl_list_search (list2, obj);
node3 = gl_list_search (list3, obj);
if (node1 == NULL)
{
ASSERT (node2 == NULL);
ASSERT (node3 == NULL);
}
else
{
ASSERT (node2 != NULL);
//.........这里部分代码省略.........
开发者ID:Distrotech,项目名称:gnulib,代码行数:101,代码来源:test-rbtree_list.c
示例7: minewall
void minewall(int in,int cn)
{
int in2,amount,co;
char buf[80];
if (!cn) {
if (!it[in].drdata[4]) {
it[in].drdata[4]=1;
switch((it[in].x+it[in].y)%3) {
case 0: it[in].sprite=15070; break;
case 1: it[in].sprite=15078; break;
case 2: it[in].sprite=15086; break;
}
}
if (it[in].drdata[3]==8) {
if ((map[it[in].x+it[in].y*MAXMAP].flags&MF_TMOVEBLOCK) || map[it[in].x+it[in].y*MAXMAP].it) {
call_item(it[in].driver,in,0,ticker+TICKS);
return;
}
it[in].sprite-=8;
it[in].drdata[3]=0;
it[in].flags|=IF_USE;
it[in].flags&=~IF_VOID;
remove_lights(it[in].x,it[in].y);
map[it[in].x+it[in].y*MAXMAP].it=in;
map[it[in].x+it[in].y*MAXMAP].flags|=MF_TSIGHTBLOCK|MF_TMOVEBLOCK;
it[in].flags|=IF_SIGHTBLOCK;
reset_los(it[in].x,it[in].y);
add_lights(it[in].x,it[in].y);
set_sector(it[in].x,it[in].y);
}
return;
}
if (ch[cn].citem) {
log_char(cn,LOG_SYSTEM,0,"Please empty your hand (mouse cursor) first.");
player_driver_dig_off(cn);
return;
}
if (it[in].drdata[3]<9) {
if (ch[cn].endurance<POWERSCALE) {
log_char(cn,LOG_SYSTEM,0,"You're too exhausted to continue digging.");
player_driver_dig_off(cn);
return;
}
ch[cn].endurance-=POWERSCALE/4-(ch[cn].prof[P_MINER]*POWERSCALE/(4*25));
it[in].drdata[3]++;
it[in].drdata[5]=0;
it[in].sprite++;
if (it[in].drdata[3]==8) {
map[it[in].x+it[in].y*MAXMAP].it=0;
map[it[in].x+it[in].y*MAXMAP].flags&=~MF_TMOVEBLOCK;
it[in].flags&=~IF_USE;
it[in].flags|=IF_VOID;
call_item(it[in].driver,in,0,ticker+TICKS*60*5+RANDOM(TICKS*60*25));
if (!RANDOM(15)) {
in2=create_item("silver");
if (!in2) elog("silver not found");
amount=RANDOM(it[in].drdata[0]*2+1)+it[in].drdata[0];
//xlog("amount=%d",amount);
if (ch[cn].prof[P_MINER]) amount+=amount*ch[cn].prof[P_MINER]/10;
//xlog("amount=%d",amount);
if (!amount && in2) {
destroy_item(in2);
}
} else if (!RANDOM(50)) {
in2=create_item("gold");
if (!in2) elog("gold not found");
amount=RANDOM(it[in].drdata[1]*2+1)+it[in].drdata[1];
if (ch[cn].prof[P_MINER]) amount+=amount*ch[cn].prof[P_MINER]/10;
if (!amount && in2) {
destroy_item(in2);
}
} else amount=in2=0;
if (amount && in2) {
it[in2].value*=amount;
*(unsigned int*)(it[in2].drdata+1)=amount;
sprintf(it[in2].description,"%d units of %s.",*(unsigned int*)(it[in2].drdata+1),it[in2].name);
if (ch[cn].flags&CF_PLAYER) dlog(cn,in2,"took from minewall");
ch[cn].citem=in2;
it[in2].carried=cn;
ch[cn].flags|=CF_ITEMS;
log_char(cn,LOG_SYSTEM,0,"You found %d units of %s.",amount,it[in2].name);
if (it[in2].drdata[0]==1) check_military_silver(cn,amount);
}
//.........这里部分代码省略.........
开发者ID:Bloodknight,项目名称:Astonia_Server_3.0,代码行数:101,代码来源:mine.c
示例8: do_kill
void
do_kill(int descr, dbref player, const char *what, int cost)
{
dbref victim;
char buf[BUFFER_LEN];
struct match_data md;
init_match(descr, player, what, TYPE_PLAYER, &md);
match_neighbor(&md);
match_me(&md);
if (Wizard(OWNER(player))) {
match_player(&md);
match_absolute(&md);
}
victim = match_result(&md);
switch (victim) {
case NOTHING:
notify(player, "I don't see that player here.");
break;
case AMBIGUOUS:
notify(player, "I don't know who you mean!");
break;
default:
if (Typeof(victim) != TYPE_PLAYER) {
notify(player, "Sorry, you can only kill other players.");
} else {
/* go for it */
/* set cost */
if (cost < tp_kill_min_cost)
cost = tp_kill_min_cost;
if (FLAGS(DBFETCH(player)->location) & HAVEN) {
notify(player, "You can't kill anyone here!");
break;
}
if (tp_restrict_kill) {
if (!(FLAGS(player) & KILL_OK)) {
notify(player, "You have to be set Kill_OK to kill someone.");
break;
}
if (!(FLAGS(victim) & KILL_OK)) {
notify(player, "They don't want to be killed.");
break;
}
}
/* see if it works */
if (!payfor(player, cost)) {
notify_fmt(player, "You don't have enough %s.", tp_pennies);
} else if ((RANDOM() % tp_kill_base_cost) < cost && !Wizard(OWNER(victim))) {
/* you killed him */
if (GETDROP(victim))
/* give him the drop message */
notify(player, GETDROP(victim));
else {
snprintf(buf, sizeof(buf), "You killed %s!", NAME(victim));
notify(player, buf);
}
/* now notify everybody else */
if (GETODROP(victim)) {
snprintf(buf, sizeof(buf), "%s killed %s! ", NAME(player), NAME(victim));
parse_oprop(descr, player, getloc(player), victim,
MESGPROP_ODROP, buf, "(@Odrop)");
} else {
snprintf(buf, sizeof(buf), "%s killed %s!", NAME(player), NAME(victim));
}
notify_except(DBFETCH(DBFETCH(player)->location)->contents, player, buf,
player);
/* maybe pay off the bonus */
if (GETVALUE(victim) < tp_max_pennies) {
snprintf(buf, sizeof(buf), "Your insurance policy pays %d %s.",
tp_kill_bonus, tp_pennies);
notify(victim, buf);
SETVALUE(victim, GETVALUE(victim) + tp_kill_bonus);
DBDIRTY(victim);
} else {
notify(victim, "Your insurance policy has been revoked.");
}
/* send him home */
send_home(descr, victim, 1);
} else {
/* notify player and victim only */
notify(player, "Your murder attempt failed.");
snprintf(buf, sizeof(buf), "%s tried to kill you!", NAME(player));
notify(victim, buf);
}
break;
}
}
}
开发者ID:giveamouse,项目名称:fbmuck,代码行数:95,代码来源:rob.c
示例9: mate
void mate(Individual *parent1, Individual *parent2,
Individual *child1, Individual *child2, int wh_product)
{
int crossover = RANDOM(wh_product - 1);
int i;
int y;
//#pragma omp sections //nowait // try num_threads(2), it may be faster than getting every thread to go in parallel, but then again, the nowait means they don't wait for the other one to finish. if we have 4 threads then 2 will (redundantly) do the first loop and 2 the second with an implied barrier at the end, but not if we do "nowait"
{
//*
//#pragma omp section
{
for (i = 0; i < crossover; i++)
{
child1->image[i] = parent1->image[i];
child2->image[i] = parent2->image[i];
}
}
//#pragma omp section
{
for (y = crossover; y < wh_product; y++)
{
child1->image[y] = parent2->image[y];
child2->image[y] = parent1->image[y];
}
}
//*/
///*
/*
//#pragma omp section
{
for (i = 0; i < crossover/2; i++)
{
child1->image[i] = parent1->image[i];
child2->image[i] = parent2->image[i];
}
}
//#pragma omp section
{
for (i = crossover/2; i < crossover; i++)
{
child1->image[i] = parent1->image[i];
child2->image[i] = parent2->image[i];
}
}
//#pragma omp section
{
for (i = crossover; i < (wh_product-crossover)/2; i++)
{
child1->image[i] = parent2->image[i];
child2->image[i] = parent1->image[i];
}
}
//#pragma omp section
{
for (i = crossover + (wh_product-crossover)/2; i < wh_product; i++)
{
child1->image[i] = parent2->image[i];
child2->image[i] = parent1->image[i];
}
}
*/
}
}
开发者ID:KyleArscott15,项目名称:distcomp,代码行数:64,代码来源:mate_omp.c
示例10: updateInput
bool Stage::step(void)
{
updateInput();
menuOn = false;
if (keys[KEYESC])
return false;
if (mouse[0] or (autoDumpOn and (bodyType == Random or bodyType == Box or bodyType == Circle)))
{
bodiesSize = smallBodiesOn ? SMALLBODYSIZE : RANDBODYSIZE;
bodiesRadius = smallBodiesOn ? SMALLHALFSIZE : RANDHALFSIZE;
switch (bodyType)
{
case Random:
if (RANDOM(0, 1))
world->makeBox(coordAllegToB2(Point(mouse_x, mouse_y)), bodiesSize);
else
world->makeCircle(coordAllegToB2(Point(mouse_x, mouse_y)), bodiesRadius);
break;
case Box:
world->makeBox(coordAllegToB2(Point(mouse_x, mouse_y)), bodiesSize);
break;
case Circle:
world->makeCircle(coordAllegToB2(Point(mouse_x, mouse_y)), bodiesRadius);
break;
case Free_Draw:
freeDraw->takePoint(Point(mouse_x, mouse_y));
break;
case Custom_Polygon:
customPolygon->takePoint(Point(mouse_x, mouse_y));
break;
case Custom_Box:
customBox->takePoint(Point(mouse_x, mouse_y));
break;
case Custom_Circle:
customCircle->takePoint(Point(mouse_x, mouse_y));
break;
}
}
else
{
if (freeDraw->On)
freeDraw->makeBody(world);
if (customBox->On)
customBox->makeBody(world);
if (customCircle->On)
customCircle->makeBody(world);
if (customPolygon->On)
customPolygon->updateView(Point(mouse_x, mouse_y));
}
if (mouse[1])
{
if (customPolygon->On)
customPolygon->makeBody(world);
else
world->makeBomb(coordAllegToB2(Point(mouse_x, mouse_y)));
}
if (keys[KEY1])
bodyType = Random;
if (keys[KEY2])
bodyType = Box;
if (keys[KEY3])
bodyType = Circle;
if (keys[KEY4])
{
bodyType = Free_Draw;
setMouseLock(false);
}
if (keys[KEY5])
{
bodyType = Custom_Box;
setMouseLock(false);
}
if (keys[KEY6])
world->destroyLastBody();
if (keys[KEY7])
world->toggleStaticMode();
if (keys[KEY8])
world->toggleSimulation();
if (keys[KEY9])
if (bodyType == Random or bodyType == Box or bodyType == Circle)
//.........这里部分代码省略.........
开发者ID:leomarques,项目名称:Dickbox,代码行数:101,代码来源:Stage.cpp
示例11: initCoarsestHypergraph
void KHMetisController::runSeqPartitioner(ParaHypergraph &hgraph,
MPI_Comm comm) {
initCoarsestHypergraph(hgraph, comm);
#ifdef DEBUG_CONTROLLER
assert(h);
#endif
h->setNumPartitions(1);
khMetisOptions[1] = numSeqRuns / numProcs + 1;
if (dispOption > 1 && myRank == 0) {
out_stream << "[KHMETIS]: " << numSeqRuns << " " << khMetisOptions[1]
<< " | ";
}
int numVertices = h->getNumVertices();
int numHedges = h->getNumHedges();
int totWt = h->getTotWeight();
int numHedgesCut;
int ubFactor = static_cast<int>(floor(kWayConstraint * 100));
int *vWeights = h->getVerWeightsArray();
int *hOffsets = h->getHedgeOffsetArray();
int *pinList = h->getPinListArray();
int *hEdgeWts = h->getHedgeWeightsArray();
int *pArray = h->getPartVectorArray();
khMetisOptions[7] = RANDOM(1, 10000000);
HMETIS_PartKway(numVertices, numHedges, vWeights, hOffsets, pinList, hEdgeWts,
numParts, ubFactor, khMetisOptions.getArray(), pArray,
&numHedgesCut);
avePartWt = static_cast<double>(totWt) / numParts;
maxPartWt = static_cast<int>(floor(avePartWt + avePartWt * kWayConstraint));
h->initCutsizes(numParts);
kWayRefiner->setMaxPartWt(maxPartWt);
kWayRefiner->setAvePartWt(avePartWt);
kWayRefiner->rebalance(*h);
#ifdef DEBUG_CONTROLLER
MPI_Barrier(comm);
for (int i = 0; i < numProcs; ++i) {
if (myRank == i)
h->checkPartitions(numParts, maxPartWt);
MPI_Barrier(comm);
}
#endif
// ###
// project partitions
// ###
initSeqPartitions(hgraph, comm);
#ifdef DEBUG_CONTROLLER
hgraph.checkPartitions(numParts, maxPartWt, comm);
#endif
DynaMem::delete_pointer<Hypergraph>(h);
}
开发者ID:falcong,项目名称:parkway,代码行数:67,代码来源:KHMetisController.cpp
示例12: cmd_steal
void cmd_steal(int cn)
{
int co,x,y,n,cnt,in=0,chance,dice,diff,m;
if (!ch[cn].prof[P_THIEF]) {
log_char(cn,LOG_SYSTEM,0,"You are not a thief, you cannot steal.");
return;
}
if (ch[cn].action!=AC_IDLE) {
log_char(cn,LOG_SYSTEM,0,"You can only steal when standing still.");
return;
}
if (ch[cn].citem) {
log_char(cn,LOG_SYSTEM,0,"Please free your hand (mouse cursor) first.");
return;
}
dx2offset(ch[cn].dir,&x,&y,NULL);
x+=ch[cn].x;
y+=ch[cn].y;
if (x<1 || x>=MAXMAP-1 || y<1 || y>=MAXMAP-1) {
log_char(cn,LOG_SYSTEM,0,"Out of map.");
return;
}
m=x+y*MAXMAP;
co=map[m].ch;
if (!co) {
log_char(cn,LOG_SYSTEM,0,"There's no one to steal from.");
return;
}
if (!can_attack(cn,co)) {
log_char(cn,LOG_SYSTEM,0,"You cannot steal from someone you are not allowed to attack.");
return;
}
if (map[m].flags&(MF_ARENA|MF_CLAN)) {
log_char(cn,LOG_SYSTEM,0,"You cannot steal inside an arena.");
return;
}
if (!(ch[co].flags&CF_PLAYER)) {
log_char(cn,LOG_SYSTEM,0,"You can only steal from players.");
return;
}
if (ch[co].driver==CDR_LOSTCON) {
log_char(cn,LOG_SYSTEM,0,"You cannot steal from lagging players.");
return;
}
if (areaID==20) {
log_char(cn,LOG_SYSTEM,0,"You cannot steal in Live Quests.");
return;
}
if (ch[co].action!=AC_IDLE || ticker-ch[co].regen_ticker<TICKS) {
log_char(cn,LOG_SYSTEM,0,"You cannot steal from someone if your victim is not standing still.");
return;
}
for (n=cnt=0; n<INVENTORYSIZE; n++) {
if (n>=12 && n<30) continue;
if ((in=ch[co].item[n]) && !(it[in].flags&IF_QUEST) && can_carry(cn,in,1)) cnt++;
}
if (!cnt) {
log_char(cn,LOG_SYSTEM,0,"You could not find anything to steal.");
return;
}
cnt=RANDOM(cnt);
for (n=cnt=0; n<INVENTORYSIZE; n++) {
if (n>=12 && n<30) continue;
if ((in=ch[co].item[n]) && !(it[in].flags&IF_QUEST) && can_carry(cn,in,1)) {
if (cnt<1) break;
cnt--;
}
}
if (n==INVENTORYSIZE) {
log_char(cn,LOG_SYSTEM,0,"You could not find anything to steal (2).");
return;
}
diff=(ch[cn].value[0][V_STEALTH]-ch[co].value[0][V_PERCEPT])/2;
chance=40+diff;
if (chance<10) {
log_char(cn,LOG_SYSTEM,0,"You'd get caught for sure. You decide not to try.");
return;
}
chance=min(chance,ch[cn].prof[P_THIEF]*3);
dice=RANDOM(100);
diff=chance-dice;
if (diff<-20) {
log_char(cn,LOG_SYSTEM,0,"%s noticed your attempt and stopped you from stealing.",ch[co].name);
ch[cn].endurance=1;
if (ch[co].flags&CF_PLAYER) {
log_char(co,LOG_SYSTEM,0,"°c3%s tried to steal from you!",ch[cn].name);
} else notify_char(co,NT_GOTHIT,cn,0,0);
//.........这里部分代码省略.........
开发者ID:Bloodknight,项目名称:Astonia_Server_3.0,代码行数:101,代码来源:prof.c
示例13: varsub
void
varsub(int qnum, int vnum, int flags)
{
static char param[11][128];
static char formats[23][128];
static FILE *lfp = NULL;
static int bInit = 0;
long *lptr;
char *ptr;
int i = 0;
DSS_HUGE tmp_date, tmp1, tmp2;
if (!bInit)
{
sprintf(formats[4], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
sprintf(formats[5], "19%s-01-01", HUGE_DATE_FORMAT);
sprintf(formats[6], "19%s-01-01", HUGE_DATE_FORMAT);
sprintf(formats[7], "0.%s", HUGE_DATE_FORMAT); /* used by q6 */
sprintf(formats[10], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
sprintf(formats[12], "19%s-01-01", HUGE_DATE_FORMAT);
sprintf(formats[14], "19%s-01-01", HUGE_DATE_FORMAT);
sprintf(formats[15], "19%s-01-01", HUGE_DATE_FORMAT);
sprintf(formats[16], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
sprintf(formats[17], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
sprintf(formats[19], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
sprintf(formats[20], "19%s-01-01", HUGE_DATE_FORMAT);
bInit = 1;
}
if (vnum == 0)
{
if ((flags & DFLT) == 0)
{
switch(qnum)
{
case 1:
sprintf(param[1], HUGE_FORMAT, UnifInt((DSS_HUGE)60,(DSS_HUGE)120,qnum));
param[2][0] = '\0';
break;
case 2:
sprintf(param[1], HUGE_FORMAT,
UnifInt((DSS_HUGE)P_SIZE_MIN, (DSS_HUGE)P_SIZE_MAX, qnum));
pick_str(&p_types_set, qnum, param[3]);
ptr = param[3] + strlen(param[3]);
while (*(ptr - 1) != ' ') ptr--;
strcpy(param[2], ptr);
pick_str(®ions, qnum, param[3]);
param[4][0] = '\0';
break;
case 3:
pick_str(&c_mseg_set, qnum, param[1]);
/*
* pick a random offset within the month of march and add the
* appropriate magic numbers to position the output functions
* at the start of March '95
*/
RANDOM(tmp_date, 0, 30, qnum);
strcpy(param[2], *(asc_date + tmp_date + 1155));
param[3][0] = '\0';
break;
case 4:
tmp_date = UnifInt((DSS_HUGE)1,(DSS_HUGE)58,qnum);
sprintf(param[1],formats[4],
93 + tmp_date/12, tmp_date%12 + 1);
param[2][0] = '\0';
break;
case 5:
pick_str(®ions, qnum, param[1]);
tmp_date = UnifInt((DSS_HUGE)93, (DSS_HUGE)97,qnum);
sprintf(param[2], formats[5], tmp_date);
param[3][0] = '\0';
break;
case 6:
tmp_date = UnifInt((DSS_HUGE)93,(DSS_HUGE)97,qnum);
sprintf(param[1], formats[6], tmp_date);
sprintf(param[2], formats[7],
UnifInt((DSS_HUGE)2, (DSS_HUGE)9, qnum));
sprintf(param[3], HUGE_FORMAT, UnifInt((DSS_HUGE)24, (DSS_HUGE)25, qnum));
param[4][0] = '\0';
break;
case 7:
tmp_date = pick_str(&nations2, qnum, param[1]);
while (pick_str(&nations2, qnum, param[2]) == tmp_date);
param[3][0] = '\0';
break;
case 8:
tmp_date = pick_str(&nations2, qnum, param[1]);
tmp_date = nations.list[tmp_date].weight;
strcpy(param[2], regions.list[tmp_date].text);
pick_str(&p_types_set, qnum, param[3]);
param[4][0] = '\0';
break;
case 9:
pick_str(&colors, qnum, param[1]);
param[2][0] = '\0';
break;
case 10:
tmp_date = UnifInt((DSS_HUGE)1,(DSS_HUGE)24,qnum);
sprintf(param[1],formats[10],
93 + tmp_date/12, tmp_date%12 + 1);
//.........这里部分代码省略.........
开发者ID:greenplum-db,项目名称:tpch,代码行数:101,代码来源:varsub.c
示例14: semi_greedy
static void
semi_greedy(data *d, int start, int *tour, int *tour_length)
{
int i, j, k;
int dim = d->numnodes;
int current, len, dist;
int *visited = MALLOC(dim, int);
/* RCL. */
struct heapelm *candidates = MALLOC(dim, struct heapelm);
int rclsize, rclused;
double total_bias, rprob, racc, rpick;
memset(visited, 0, dim * sizeof(int));
len = 0;
current = start;
for (i = 0; i < dim - 1; i++) {
visited[current] = 1;
tour[i] = current;
/* Define RCL size. */
rclused = 0;
if (RANDOM_UNIT() < 0.05) rclsize = dim - i - 2;
else rclsize = RANDOM(((dim - i - 2) / 4) + 1);
rclsize++;
DEBUG("RCL size: %d\n", rclsize);
/* Define RCL. */
for (j = 0; j < dim; j++) {
if (!visited[j]) {
dist = d->dist(d, current, j);
if (rclused < rclsize) {
candidates[rclused].num = j;
candidates[rclused].cost = dist;
rclused++;
if (rclused == rclsize) {
heap_build_max(candidates, rclsize);
}
} else if (dist < candidates[0].cost) {
heap_replace_root(candidates, rclsize, j, dist);
}
}
}
DEBUG("RCL used: %d\n", rclused);
heap_dump(candidates, rclused);
/* Pick RCL element based on logarithmic bias. */
#define BIAS_LOG(r) (1/log((r)+1))
#define BIAS_LINEAR(r) (1./(r))
#define BIAS_EXP(r) (1/exp(r))
#define BIAS_RANDOM(r) 1
#define BIAS(r) BIAS_LOG(r)
total_bias = 0;
for (j = 1; j <= rclused; j++) {
total_bias += BIAS(j);
}
racc = 0;
rpick = RANDOM_UNIT();
#if DEBUGGING
for (j = rclused; j >= 1; j--) {
DEBUG("%f ", BIAS(j) / total_bias);
}
DEBUG("\nR: %f\n", rpick);
#endif
for (j = rclused, k = 1; j >= 0; j--, k++) {
rprob = BIAS(k) / total_bias;
if (rprob + racc > rpick) {
break;
}
racc += rprob;
}
DEBUG("Picked: j = %d, %d %d\n\n", j,
candidates[j-1].num, candidates[j-1].cost);
current = candidates[j - 1].num;
len += candidates[j - 1].cost;
}
tour[i] = current;
len += d->dist(d, current, start);
*tour_length = len;
free(visited);
free(candidates);
#undef BIAS
#undef BIAS_LOG
}
开发者ID:gpolo,项目名称:STSP,代码行数:91,代码来源:naive_grasp.c
示例15: mcp_frame_output_mesg
//.........这里部分代码省略.........
if (anarg->value) {
McpArgPart *ap = anarg->value;
while (ap) {
p = ap->value;
while (*p) {
if (*p == '\n' || *p == '\r') {
McpArgPart *nu = (McpArgPart *) malloc(sizeof(McpArgPart));
nu->next = ap->next;
ap->next = nu;
*p++ = '\0';
nu->value = string_dup(p);
ap->value = (char *) realloc(ap->value, strlen(ap->value) + 1);
ap = nu;
p = nu->value;
} else {
p++;
}
}
ap = ap->next;
}
}
}
/* Build the initial message string */
out = outbuf;
bufrem = outbuf + sizeof(outbuf) - out;
for (anarg = msg->args; anarg; anarg = anarg->next) {
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
if (!anarg->value) {
anarg->was_shown = 1;
snprintf(out, bufrem, " %s: %s", anarg->name, MCP_ARG_EMPTY);
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
} else {
int totlen = strlen(anarg->value->value) + strlen(anarg->name) + 5;
if (anarg->value->next || totlen > ((BUFFER_LEN - (out - outbuf)) / 2)) {
/* Value is multi-line or too long. Send on separate line(s). */
mlineflag = 1;
anarg->was_shown = 0;
snprintf(out, bufrem, " %s*: %s", anarg->name, MCP_ARG_EMPTY);
} else {
anarg->was_shown = 1;
snprintf(out, bufrem, " %s: ", anarg->name);
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
msgarg_escape(out, bufrem, anarg->value->value);
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
}
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
}
}
/* If the message is multi-line, make sure it has a _data-tag field. */
if (mlineflag) {
snprintf(datatag, sizeof(datatag), "%.8lX", (unsigned long)(RANDOM() ^ RANDOM()));
snprintf(out, bufrem, " %s: %s", MCP_DATATAG, datatag);
out += strlen(out);
bufrem = outbuf + sizeof(outbuf) - out;
}
/* Send the initial line. */
SendText(mfr, outbuf);
SendText(mfr, "\r\n");
if (mlineflag) {
/* Start sending arguments whose values weren't already sent. */
/* This is usually just multi-line argument values. */
for (anarg = msg->args; anarg; anarg = anarg->next) {
if (!anarg->was_shown) {
McpArgPart *ap = anarg->value;
while (ap) {
*outbuf = '\0';
snprintf(outbuf, sizeof(outbuf), "%s* %s %s: %s", MCP_MESG_PREFIX, datatag, anarg->name, ap->value);
SendText(mfr, outbuf);
SendText(mfr, "\r\n");
if (!--flushcount) {
FlushText(mfr);
flushcount = 8;
}
ap = ap->next;
}
}
}
/* Let the other side know we're done sending multi-line arg vals. */
snprintf(outbuf, sizeof(outbuf), "%s: %s", MCP_MESG_PREFIX, datatag);
SendText(mfr, outbuf);
SendText(mfr, "\r\n");
}
return EMCP_SUCCESS;
}
开发者ID:hyena,项目名称:fuzzball,代码行数:101,代码来源:mcp.c
示例16: get_randomized_dir
/**
* Returns a random direction (1..8) similar to a given direction.
* @param dir The exact direction.
* @return The randomized direction. */
int get_randomized_dir(int dir)
{
return absdir(dir + RANDOM() % 3 + RANDOM() % 3 - 2);
}
开发者ID:atrinik,项目名称:clearhaven-mine,代码行数:8,代码来源:utils.c
示例17: main
int
main (int argc, char *argv[])
{
gl_oset_t set1;
gl_list_t set2;
set_program_name (argv[0]);
/* Allow the user to provide a non-default random seed on the command line. */
if (argc > 1)
srand (atoi (argv[1]));
{
size_t initial_size = RANDOM (20);
size_t i;
unsigned int repeat;
/
|
请发表评论