public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Rick Dearman <rick@ricken.demon.co.uk>
To: help-gcc@gnu.org
Subject: Re: gcc newbie: rand()
Date: Sun, 12 Dec 1999 15:35:00 -0000	[thread overview]
Message-ID: <385430E6.9133F3C5@ricken.demon.co.uk> (raw)
In-Reply-To: <3850EBFA.BE9086DC@singnet.com.sg>

Try this:

#include <stdlib.h>
#include <time.h>

int
get_rand(int range)
{
  int mrand;
  mrand = (int)((double)rand() / ((double)RAND_MAX +1) * range);
  return mrand;
}

int
main(void)
{
  float x;
  int n;

  srand((unsigned int)time((time_t *)NULL));

  x = (float)( get_rand(5) / 32768.0 );
  n = 1 + (int) (6*x);
	
  printf("x = %d\n", n);

  return 0;

}

WARNING: multiple messages have this Message-ID
From: Rick Dearman <rick@ricken.demon.co.uk>
To: help-gcc@gnu.org
Subject: Re: gcc newbie: rand()
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <385430E6.9133F3C5@ricken.demon.co.uk> (raw)
Message-ID: <19991231222400.Nuwc1UjRSEIh4RhEpSCj0EWAyL-TDHl8CPjB1h-HLhU@z> (raw)
In-Reply-To: <3850EBFA.BE9086DC@singnet.com.sg>

Try this:

#include <stdlib.h>
#include <time.h>

int
get_rand(int range)
{
  int mrand;
  mrand = (int)((double)rand() / ((double)RAND_MAX +1) * range);
  return mrand;
}

int
main(void)
{
  float x;
  int n;

  srand((unsigned int)time((time_t *)NULL));

  x = (float)( get_rand(5) / 32768.0 );
  n = 1 + (int) (6*x);
	
  printf("x = %d\n", n);

  return 0;

}

  parent reply	other threads:[~1999-12-12 15:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-10  4:04 Joachim Bauernberger
1999-12-10  4:40 ` Jan Dvorak
1999-12-31 22:24   ` Jan Dvorak
1999-12-10 22:18 ` Martin Kahlert
1999-12-31 22:24   ` Martin Kahlert
1999-12-12 14:41 ` Patrick Block
1999-12-31 22:24   ` Patrick Block
1999-12-12 15:29 ` Rick Dearman
1999-12-31 22:24   ` Rick Dearman
1999-12-12 15:35 ` Rick Dearman [this message]
1999-12-31 22:24   ` Rick Dearman
1999-12-31 22:24 ` Joachim Bauernberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=385430E6.9133F3C5@ricken.demon.co.uk \
    --to=rick@ricken.demon.co.uk \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).