From owner-freebsd-questions Tue Feb 16 22: 9:40 1999 Delivered-To: freebsd-questions@freebsd.org Received: from superior.mooseriver.com (superior.mooseriver.com [208.138.31.113]) by hub.freebsd.org (Postfix) with ESMTP id 8123610E0B for ; Tue, 16 Feb 1999 22:09:36 -0800 (PST) (envelope-from jgrosch@superior.mooseriver.com) Received: (from jgrosch@localhost) by superior.mooseriver.com (8.8.8/8.8.8) id WAA05253; Tue, 16 Feb 1999 22:09:31 -0800 (PST) (envelope-from jgrosch) Message-ID: <19990216220931.B5145@mooseriver.com> Date: Tue, 16 Feb 1999 22:09:31 -0800 From: Josef Grosch To: Patrick Seal , freebsd-questions@FreeBSD.ORG Subject: Re: Random Numbers Reply-To: jgrosch@mooseriver.com References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.91.1i In-Reply-To: ; from Patrick Seal on Tue, Feb 16, 1999 at 09:36:40PM -0500 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tue, Feb 16, 1999 at 09:36:40PM -0500, Patrick Seal wrote: > In this the best way to get a number from one to ten in C: > > #include > > randn(float value) { > value = random() * value / 2147483648.0; > return(value); > } > > n = randn(10.0) + 1 > > printf("%i", n); > > I realize it's platform dependent. Is there a better way? You should seed the random number generator first. Try the following; int randn() { int rc = 0; pid_t pid = getpid(); time_t now = 0; long value = 0L; unsigned long seed = 0L; struct timeval tp; struct timezone tzp; gettimeofday(&tp, &tzp); now = tp.tv_sec; /* unnecessary but nice during debugging */ seed = (unsigned long)(now / pid); srandom(seed); value = random(); rc = (value % 10); return (rc); } This is OK for a quick hack. Play around with it. You should also consult the man pages for random Josef -- Josef Grosch | Another day closer to a | FreeBSD 3.0 jgrosch@MooseRiver.com | Micro$oft free world | UNIX for the masses To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message