开发者

randomize functions

  void main()
 {

  randomize();
  char city[][10]={"DEL","CHN","KOL",开发者_如何转开发"BOM","BNG"};
  int i,fly;
  for(i=0;i<3;i++)
  {
   fly=random(2)+1;
   cout<<city[fly]<<":";
  }
 }

What is the output of the following code snippet?


If you want the same output every time you run a program using random number generators, you want to seed the generator with the same value every time. For example:

#include <cstdlib>
#include <iostream>
using namespace std:

int main() {
   srand( 42 );  // generator always seeded with same value
   for ( int i = 0; i < 10; i++ ) {
      cout << rand() << endl;
   }
}

always produces the same sequence.


random(2) is either 0 or 1 Therefore fly = random(2) + 1 is either 1 or 2 city[fly] is either "CHN" or "KOL" since loop repeats thrice, the possible answers are

  • CHN:CHN:CHN
  • CHN:CHN:KOL
  • CHN:KOL:CHN
  • CHN:KOL:KOL
  • KOL:CHN:CHN
  • KOL:CHN:KOL
  • KOL:KOL:CHN
  • KOL:KOL:KOL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜