Saturday, March 1, 2014

How to Generate Random Occurance of a Number in C++?

You can use Microsoft's Inbuilt Data-type Called Random in C++/CLI

// RandomNumberOccurance.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
Random ^r = gcnew Random();//This is inbuilt data type in MS VC++
int randomnumber;
int throws = 0;
do{
randomnumber = r->Next(1, 7);
Console::WriteLine(randomnumber);
throws++;
} while (randomnumber != 6);

Console::Write("You took");
Console::Write(throws);
Console::Write("To get 6");
Console::ReadKey();
    return 0;
}

This program shows random occurance of number 6 by taking interval from 1 to 7 , thorws shows the count at which time 6 occurs in random tries

No comments:

Post a Comment