~ubuntu-branches/ubuntu/natty/freeciv/natty-proposed

« back to all changes in this revision

Viewing changes to utility/rand.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams, Karl Goetz, Clint Adams
  • Date: 2010-02-23 22:09:02 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100223220902-s3spqi1x4e190y0t
Tags: 2.2.0-1
[ Karl Goetz ]
* Remove civserver files in /etc/ggzd/ (Closes: 523772, 517787)
* Adding ${misc:Depends} to all binary packages (lintian warnings)

[ Clint Adams ]
* New upstream version.
  - Drop data_dsc_use_bindir.diff (binary pathnames have changed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
  directly representable in type RANDOM_TYPE, so we do instead:
79
79
         divisor = MAX_UINT32/size
80
80
*************************************************************************/
81
 
RANDOM_TYPE myrand_debug(RANDOM_TYPE size,
82
 
                     const char *called_as, int line, const char *file) 
 
81
RANDOM_TYPE myrand_debug(RANDOM_TYPE size, const char *called_as,
 
82
                         int line, const char *file) 
83
83
{
84
84
  RANDOM_TYPE new_rand, divisor, max;
85
85
  int bailout = 0;
110
110
    rand_state.v[rand_state.x] = new_rand;
111
111
 
112
112
    if (++bailout > 10000) {
113
 
      freelog(LOG_ERROR, "%s(%lu) = %lu bailout at line %d of %s", 
114
 
            called_as, (unsigned long)size, (unsigned long)new_rand, line, file);
 
113
      freelog(LOG_ERROR, "%s(%lu) = %lu bailout at %s:%d", 
 
114
            called_as, (unsigned long)size, (unsigned long)new_rand, file, line);
115
115
      new_rand = 0;
116
116
      break;
117
117
    }
124
124
    new_rand = 0;
125
125
  }
126
126
 
127
 
  freelog(LOG_RAND, "%s(%lu) = %lu at line %d of %s",
128
 
            called_as, (unsigned long)size, (unsigned long)new_rand, line, file);
 
127
  freelog(LOG_RAND, "%s(%lu) = %lu at %s:%d",
 
128
            called_as, (unsigned long)size, (unsigned long)new_rand, file, line);
129
129
 
130
130
  return new_rand;
131
131
212
212
 
213
213
/*************************************************************************
214
214
  Test one aspect of randomness, using n numbers.
215
 
  Reports results to LOG_NORMAL; with good randomness, behaviourchange
 
215
  Reports results to LOG_TEST; with good randomness, behaviourchange
216
216
  and behavioursame should be about the same size.
217
217
  Tests current random state; saves and restores state, so can call
218
218
  without interrupting current sequence.
242
242
    }
243
243
    old_value = new_value;
244
244
  }
245
 
  freelog(LOG_NORMAL, "test_random1(%d) same: %d, change: %d",
 
245
  freelog(LOG_TEST, "test_random1(%d) same: %d, change: %d",
246
246
          n, behavioursame, behaviourchange);
247
247
 
248
248
  /* restore state: */
249
249
  set_myrand_state(saved_state);
250
250
}
 
251
 
 
252
/*************************************************************************
 
253
  Local pseudo-random function for repeatedly reaching the same result,
 
254
  instead of myrand().  Primarily needed for tiles.
 
255
 
 
256
  Use an invariant equation for seed.
 
257
  Result is 0 to (size - 1).
 
258
*************************************************************************/
 
259
RANDOM_TYPE myrandomly_debug(RANDOM_TYPE seed, RANDOM_TYPE size,
 
260
                             const char *called_as, int line, const char *file)
 
261
{
 
262
  RANDOM_TYPE result;
 
263
 
 
264
#define LARGE_PRIME (10007)
 
265
#define SMALL_PRIME (1009)
 
266
 
 
267
  /* Check for overflow and underflow */
 
268
  assert(seed < MAX_UINT32 / LARGE_PRIME);
 
269
  assert(size < SMALL_PRIME);
 
270
  assert(size > 0);
 
271
  result = ((seed * LARGE_PRIME) % SMALL_PRIME) % size;
 
272
 
 
273
  freelog(LOG_RAND, "%s(%lu,%lu) = %lu at %s:%d",
 
274
          called_as, (unsigned long)seed, (unsigned long)size,
 
275
          (unsigned long)result, file, line);
 
276
 
 
277
  return result;
 
278
}