delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/11/15/14:30:22

Date: Sun, 15 Nov 1998 14:15:44 -0500 (EST)
Message-Id: <199811151915.OAA23094@indy.delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: djgpp-workers AT delorie DOT com
Subject: src/libc/ansi/stdlib/rand.c
Reply-To: djgpp-workers AT delorie DOT com

* new multiplier from K.B. Williams
* auto-initialize if the user forgets to

Comments?

/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <stdlib.h>
#include <pc.h>
#include <go32.h>
#include <libc/farptrgs.h>

static unsigned long long next = 0;
static int srand_called = 0;

int
rand(void)
{
  if (!srand_called)
  {
    unsigned int lsb, msb, tics;
    outportb(0x43, 0x00);
    lsb = inportb(0x40);
    msb = inportb(0x40);
    tics = _farpeekl(_dos_ds, 0x46c);
    srand(lsb | (msb<<8) || (((long long)tics)<<16));
  }

  next = next * 6364136223846793005LL + 1;
  /* was: next = next * 0x5deece66dLL + 11; */
  return (int)((next >> 16) & RAND_MAX);
}

void
srand(unsigned seed)
{
  next = seed;
  srand_called = 1;
  rand();
  rand();
  rand();
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019