﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-天狼紫枫-文章分类-C/C++笔记</title><link>http://www.cppblog.com/sionxu1986/category/4597.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 03:55:31 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 03:55:31 GMT</pubDate><ttl>60</ttl><item><title>[C++]用rand()和srand()产生伪随机数的方法</title><link>http://www.cppblog.com/sionxu1986/articles/27088.html</link><dc:creator>徐光宇</dc:creator><author>徐光宇</author><pubDate>Wed, 27 Jun 2007 10:37:00 GMT</pubDate><guid>http://www.cppblog.com/sionxu1986/articles/27088.html</guid><wfw:comment>http://www.cppblog.com/sionxu1986/comments/27088.html</wfw:comment><comments>http://www.cppblog.com/sionxu1986/articles/27088.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sionxu1986/comments/commentRss/27088.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sionxu1986/services/trackbacks/27088.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">标准库&lt;cstdlib&gt;（被包含于&lt;iostream&gt;中）提供两个帮助生成伪随机数的函数：<br><br>函数一：int rand(void)；<br>从srand (seed)中指定的seed开始，返回一个[seed, RAND_MAX（0x7fff）)间的随机整数。<br><br>函数二：void srand(unsigned seed)；<br>参数seed是rand()的种子，用来初始化rand()的起始值。<br><br>一个通用的公式是：<br>要取得[a,b)之间的随机整数，使用（rand() % (b-a)）+ a （结果值将含a不含b）。<br>在a为0的情况下，简写为rand() % b。<br><br>用rand() / double(RAND_MAX)可以取得0～1之间的浮点数（注意，不同于整型时候的公式，是除以，不是求模）这不是伪随机浮点数最好的实现方法，不过可以将就着用用&#8230;<br></span><img src ="http://www.cppblog.com/sionxu1986/aggbug/27088.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sionxu1986/" target="_blank">徐光宇</a> 2007-06-27 18:37 <a href="http://www.cppblog.com/sionxu1986/articles/27088.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[C++]cin导致刷屏的问题</title><link>http://www.cppblog.com/sionxu1986/articles/27087.html</link><dc:creator>徐光宇</dc:creator><author>徐光宇</author><pubDate>Wed, 27 Jun 2007 10:33:00 GMT</pubDate><guid>http://www.cppblog.com/sionxu1986/articles/27087.html</guid><wfw:comment>http://www.cppblog.com/sionxu1986/comments/27087.html</wfw:comment><comments>http://www.cppblog.com/sionxu1986/articles/27087.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sionxu1986/comments/commentRss/27087.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sionxu1986/services/trackbacks/27087.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">在cin&gt;&gt;&lt;int/float&gt;时 如果我们输入一个字符 就会导致刷屏的结果 这是因为非数字字符无法被cin接收而一直停留在缓冲区，导致下一次cin时直接从缓存读数 但字符无法读取结果导致无限循环 这被很多人认为成库的bug<br><br>解决方法：<br><br>1&gt; 在cin&gt;&gt;&lt;int/float&gt;后加cin.ignore();&nbsp; cin.clear();<br><br>cin.ignore（）方法cin.ignore(&nbsp;&nbsp; 5,&nbsp;&nbsp; 'c'&nbsp;&nbsp; )&nbsp;&nbsp; 的是从输入流（cin）中提取字符，提取的字符被忽略（ignore），不被使用。每抛弃一个字符，它都要计数和比较字符：如果计数值达到5或者被抛弃的字符是'c'，则cin.ignore()&nbsp;&nbsp; 函数执行终止；否则，它继续等待。&nbsp; 它的一个常用功能就是用来清除以回车结束的输入缓冲区的内容，消除上一次输入对下一次输入的影响。比如可以这么用：cin.ignore(&nbsp;&nbsp; 1024,&nbsp;&nbsp; '\n'&nbsp;&nbsp; );，通常把第一个参数设置得足够大，这样实际上总是只有第二个参数&nbsp;&nbsp; '\n'&nbsp;&nbsp; 起作用，所以这一句就是把回车（包括回车）之前的所以字符从输入缓冲（流）中清除出去。<br><br>cin.clear用法如果输入发生错误发生，那么流状态既被标记为错误，你必须清除这些错误状态，以使你的程序能正确适当地继续运行。要清除错误状态，需使用clear()函数。此函数带一个参数，它是你将要设为当前状态的标志值。，只要将ios::goodbit作为实参。<br><br>2&gt; 在cin&gt;&gt;&lt;int/float&gt;后加fflush(stdin);（需要stdio.h）<br><br>fflush(stdin)刷新标准输入缓冲区<br><br></span> <img src ="http://www.cppblog.com/sionxu1986/aggbug/27087.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sionxu1986/" target="_blank">徐光宇</a> 2007-06-27 18:33 <a href="http://www.cppblog.com/sionxu1986/articles/27087.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>