荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Version (音乐,美的最初与最终), 信区: Program
标  题: Re: 一个随机数的问题
发信站: 荔园晨风BBS站 (Sun May 25 13:35:40 2003), 站内信件

 是的。重复是个问题。但是我解决不了。
 所以我找来了个程序给你。自己看下。也许对你有帮助的。

 /*
 * The authors of this software are Don Mitchell and Matt Blaze.
 *              Copyright (c) 1995 by AT&T.
 * Permission to use, copy, and modify this software without fee
 * is hereby granted, provided that this entire notice is included in
 * all copies of any software which is or includes a copy or
 * modification of this software and in all copies of the supporting
 * documentation for such software.
 *
 * This software may be subject to United States export controls.
 *
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
 */

#include <signal.h>
#include <setjmp.h>
#include <sys/time.h>
#include <math.h>
#include <stdio.h>

static jmp_buf env;
static unsigned count;
static unsigned ocount;
static unsigned buffer;

static int
tick()
{
        struct itimerval it, oit;

        timerclear(&it.it_interval);
        it.it_value.tv_sec = 0;
        it.it_value.tv_usec = 16665;
        if (setitimer(ITIMER_REAL, &it, &oit) < 0)
                perror("tick");
}

static void
interrupt()
{
        if (count)
                longjmp(env, 1);
        (void) signal(SIGALRM, interrupt);
        tick();
}

static unsigned long
roulette()
{

        if (setjmp(env)) {
                count ^= (count>>3) ^ (count>>6) ^ ocount;
                count &= 0x7;
                ocount=count;
                buffer = (buffer<<3) ^ count;
                return buffer;
        }
        (void) signal(SIGALRM, interrupt);
        count = 0;
        tick();
        for (;;)
                count++;        /* about 1 MHz on VAX 11/780 */
}

unsigned long
truerand()
{

        count=0;
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        (void) roulette();
        return roulette();


n_truerand(n)
int n;
{
        int slop, v;

        slop = 0x7FFFFFFF % n;
        do {
                v = truerand() >> 1;
        } while (v <= slop);
        return v % n;
}

--
                      *
          *                                  *
                          *             *
                      no more to say
                  ★     just wish you   ★
                            good luck

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.50]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店