⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randrange.kirby.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- Mirrored from c-faq.com/lib/randrange.kirby.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --><head><title></title></head><body>From: Lawrence Kirby<br>Newsgroups: comp.lang.c<br>Subject: Re: why aren't my random numbers random ?<br>Date: Wed, 09 Jun 99 01:38:49 GMT<br>Message-ID: &lt;928892329snz@genesis.demon.co.uk&gt;<p>In article &lt;7jm48e$606$1@eskinews.eskimo.com&gt;scs@eskimo.com "Steve Summit" writes:<br>...<br>&gt; When N is close to RAND_MAX...  about the<br>&gt; only thing you can do is to call rand() multiple times,<br>&gt; discarding certain values:<br>&gt;<br>&gt; [code omitted]<p>This is very similar to something I've posted a few times.<p><pre>#include &lt;stdlib.h&gt;int randnum(int range){    int divisor = RAND_MAX / range;    int threshold = RAND_MAX - RAND_MAX % range;    int randval;    while ((randval = rand()) &gt;= threshold)        ;    return randval / divisor;}</pre><p>In theory this could loop indefinitely. However the chance of exiting theloop on each iteration is always greater than 0.5 and usually much closerto 1. Therefore the chances of the loop continuing become vanishinglysmall after relatively few iterations (after 100 iterations it isprobably much less than 1 chance in 10^^30). This does assume howeverthat rand() isn't severely broken i.e. that it doesn't churn out largegroups of large numbers. However even poor PRNG algorithms don'ttend to do that (which leaves a genuinely buggy implementation).<p><pre>-- -----------------------------------------Lawrence Kirby | fred@genesis.demon.co.ukWilts, England | 70734.126@compuserve.com-----------------------------------------</pre></body><!-- Mirrored from c-faq.com/lib/randrange.kirby.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -