问题描述
如何在vs2005下使用Random类,我在自定义的文件中包含#include<Random.h>出错,谢谢指点
解决方案
解决方案二:
//ExampleoftheRandom::Next()methods.usingnamespaceSystem;//Generaterandomnumberswithnoboundsspecified.voidNoBoundsRandoms(intseed){Console::WriteLine("nRandomobject,seed={0},nobounds:",seed);Random^randObj=gcnewRandom(seed);//Generatesixrandomintegersfrom0toint.MaxValue.for(intj=0;j<6;j++)Console::Write("{0,11}",randObj->Next());Console::WriteLine();}//Generaterandomnumberswithanupperboundspecified.voidUpperBoundRandoms(intseed,intupper){Console::WriteLine("nRandomobject,seed={0},upperbound={1}:",seed,upper);Random^randObj=gcnewRandom(seed);//Generatesixrandomintegersfrom0totheupperbound.for(intj=0;j<6;j++)Console::Write("{0,11}",randObj->Next(upper));Console::WriteLine();}//Generaterandomnumberswithbothboundsspecified.voidBothBoundsRandoms(intseed,intlower,intupper){Console::WriteLine("nRandomobject,seed={0},lower={1},upper={2}:",seed,lower,upper);Random^randObj=gcnewRandom(seed);//Generatesixrandomintegersfromthelowerto//upperbounds.for(intj=0;j<6;j++)Console::Write("{0,11}",randObj->Next(lower,upper));Console::WriteLine();}intmain(){Console::WriteLine("ThisexampleoftheRandom::Next()methodsn""generatesthefollowingoutput.n");Console::WriteLine("CreateRandomobjectsallwiththesameseedand""generatensequencesofnumberswithdifferent""bounds.Notetheeffectnthatthevarious""combinationsofboundshaveonthesequences.");NoBoundsRandoms(234);UpperBoundRandoms(234,Int32::MaxValue);UpperBoundRandoms(234,2000000000);UpperBoundRandoms(234,200000000);BothBoundsRandoms(234,0,Int32::MaxValue);BothBoundsRandoms(234,Int32::MinValue,Int32::MaxValue);BothBoundsRandoms(234,-2000000000,2000000000);BothBoundsRandoms(234,-200000000,200000000);BothBoundsRandoms(234,-2000,2000);}/*ThisexampleoftheRandom::Next()methodsgeneratesthefollowingoutput.CreateRandomobjectsallwiththesameseedandgeneratesequencesofnumberswithdifferentbounds.Notetheeffectthatthevariouscombinationsofboundshaveonthesequences.Randomobject,seed=234,nobounds:2091148258102495502371127334410819171831833298756109460588Randomobject,seed=234,upperbound=2147483647:2091148258102495502371127334410819171831833298756109460588Randomobject,seed=234,upperbound=2000000000:194753358095456375166242492210076138961707392518101943116Randomobject,seed=234,upperbound=200000000:194753358954563756624249210076138917073925110194311Randomobject,seed=234,lower=0,upper=2147483647:2091148258102495502371127334410819171831833298756109460588Randomobject,seed=234,lower=-2147483648,upper=2147483647:2034812868-97573602-724936960163507181519113864-1928562472Randomobject,seed=234,lower=-2000000000,upper=2000000000:1895067160-90872498-675150156152277931414785036-1796113767Randomobject,seed=234,lower=-200000000,upper=200000000:189506716-9087250-675150161522779141478503-179611377Randomobject,seed=234,lower=-2000,upper=2000:1895-91-676151414-1797*/
解决方案三:
为什么使用Rodom类的例子怎么都在CLR中呢?如果在win32控制台中写,usingnamespaceSystem;则编译错误啊,显示未d:programmepspsexamp2.cpp(4):errorC2871:“System”:具有该名称的命名空间不存在请哪位高人指点
解决方案四:
win32可以用#include<stdlib.h>#include<stdio.h>#include<time.h>intmain(void){inti;//Seedtherandom-numbergeneratorwithcurrenttimesothat//thenumberswillbedifferenteverytimewerun.//srand((unsigned)time(NULL));//Display10numbers.for(i=0;i<10;i++)printf("%6dn",rand());printf("n");//Usually,youwillwanttogenerateanumberinaspecificrange,//suchas0to100,likethis:{intRANGE_MIN=0;intRANGE_MAX=100;for(i=0;i<10;i++){intrand100=(((double)rand()/(double)RAND_MAX)*RANGE_MAX+RANGE_MIN);printf("%6dn",rand100);}}}