Random
cat random_test.cpp
// random_test.cpp
// console application
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
#include "random_test.h"
// Generate random numbers with no bounds specified.
void NoBoundsRandoms( int seed )
{
Console::WriteLine( "\nRandom object, seed = {0}, no bounds:", seed );
Random^ randObj = gcnew Random( seed );
// Generate six random integers from 0 to int.MaxValue.
for ( int j = 0; j < 6; j++ )
Console::Write( "{0,11} ", randObj->Next() );
Console::WriteLine();
}
// Generate random numbers with an upper bound specified.
void UpperBoundRandoms( int seed, int upper )
{
Console::WriteLine( "\nRandom object, seed = {0}, upper bound = {1}:", seed, upper );
Random^ randObj = gcnew Random( seed );
// Generate six random integers from 0 to the upper bound.
for ( int j = 0; j < 6; j++ )
Console::Write( "{0,11} ", randObj->Next( upper ) );
Console::WriteLine();
}
// Generate random numbers with both bounds specified.
void BothBoundsRandoms( int seed, int lower, int upper )
{
Console::WriteLine( "\nRandom object, seed = {0}, lower = {1}, upper = {2}:", seed, lower, upper );
Random^ randObj = gcnew Random( seed );
// Generate six random integers from the lower to
// upper bounds.
for ( int j = 0; j < 6; j++ )
Console::Write( "{0,11} ", randObj->Next( lower, upper ) );
Console::WriteLine();
}
// Generate random numbers from the specified Random object.
void RunIntNDoubleRandoms( Random^ randObj )
{
// Generate the first six random integers.
for ( int j = 0; j < 6; j++ )
Console::Write( " {0,10} ", randObj->Next() );
Console::WriteLine();
// Generate the first six random doubles.
for ( int j = 0; j < 6; j++ )
Console::Write( " {0:F8} ", randObj->NextDouble() );
Console::WriteLine();
}
// Create a Random object with the specified seed.
void FixedSeedRandoms( int seed )
{
Console::WriteLine( "\nRandom numbers from a Random object with seed = {0}:", seed );
Random^ fixRand = gcnew Random( seed );
RunIntNDoubleRandoms( fixRand );
}
// Create a random object with a timer-generated seed.
void AutoSeedRandoms()
{
// Wait to allow the timer to advance.
System::Threading::Thread::Sleep( 1 );
Console::WriteLine( "\nRandom numbers from a Random object "
"with an auto-generated seed:" );
Random^ autoRand = gcnew Random;
RunIntNDoubleRandoms( autoRand );
}
int main(array<System::String ^> ^args){
Console::WriteLine( "This example of the Random::Next( ) methods\n"
"generates the following output.\n" );
Console::WriteLine( "Create Random objects all with the same seed and "
"generate\nsequences of numbers with different "
"bounds. Note the effect\nthat the various "
"combinations of bounds have on the sequences." );
NoBoundsRandoms( 234 );
UpperBoundRandoms( 234, Int32::MaxValue );
UpperBoundRandoms( 234, 2000000000 );
UpperBoundRandoms( 234, 200000000 );
UpperBoundRandoms( 234, 2000 );
UpperBoundRandoms( 234, 10 );
BothBoundsRandoms( 234, 0, Int32::MaxValue );
BothBoundsRandoms( 234, Int32::MinValue, Int32::MaxValue );
BothBoundsRandoms( 234, -2000000000, 2000000000 );
BothBoundsRandoms( 234, -200000000, 200000000 );
BothBoundsRandoms( 234, -2000, 2000 );
BothBoundsRandoms( 234, -10, 10 );
Console::WriteLine( "This example of the Random class constructors and Random"
"::NextDouble( ) \ngenerates the following output.\n" );
Console::WriteLine( "Create Random objects, and then generate and "
"display six integers and \nsix doubles from each." );
FixedSeedRandoms( 123 );
FixedSeedRandoms( 123 );
FixedSeedRandoms( 456 );
FixedSeedRandoms( 456 );
AutoSeedRandoms();
AutoSeedRandoms();
AutoSeedRandoms();
return EXIT_SUCCESS;
}
cat AssemblyInfo.cpp
// AssemblyInfo.cpp
//
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
//
// General Assembly Information
//
[assembly:AssemblyCompanyAttribute("smnb")];
[assembly:AssemblyCopyrightAttribute("Copyright smnb(c) 2007")];
[assembly:AssemblyTrademarkAttribute("smnb")];
[assembly:AssemblyProductAttribute("AssemblyInfo_test")];
[assembly: AssemblyFileVersion ("2.1.1852.0")];
[assembly: AssemblyInformationalVersion("1.2")] ; // Win32 version visible in Property
[assembly:AssemblyTitleAttribute("AssemblyInfo_test")];
[assembly:AssemblyDescriptionAttribute("This is a test.")];
// Assembly Version (Major Version).(Minor Version).(Build Number).(Revision)
//[assembly:AssemblyVersionAttribute("1.0.*")]; // Build and Revision are using default.
// Build will be number of days since 2000/1/1
// Revision will be half of number of seconds since midnight of the day.
// ... doesn't work outside Visual Studio probably..
[assembly:AssemblyVersionAttribute("1.0.1.0")]; // Build and Revision are using default.
[assembly:AssemblyConfigurationAttribute("Release")]; // or debug
[assembly:AssemblyCultureAttribute("")]; // don't define this for main assembly
[assembly:ComVisible(false)]; // COM can't refer types in this Assembly
[assembly:CLSCompliantAttribute(true)];// Class marked as CLS compliant.
// study http://www.microsoft.com/japan/msdn/security/guidance/secmod81.mspx
[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)];
cat makefile
# makefile for random_test
# console application
.PHONY: all test mmm clean
.SUFFIXES: .exe .obj .txt .resources .dll
TARGET=random_test
CULTUREDIRS=
DLLS=
all: $(TARGET).exe $(DLLS)
LOPT=
OBJS=$(TARGET).obj AssemblyInfo.obj
$(TARGET).obj: $(TARGET).cpp
AssemblyInfo.obj: AssemblyInfo.cpp
meta: $(TARGET).exe
ILDasm /out:$@ /metadata $<
$(TARGET).exe: $(OBJS)
link $^ /out:$@ $(LOPT)
.cpp.obj:
cl -c -Wall -Od -clr:safe $<
test: $(TARGET).exe
./$(TARGET).exe
clean:
@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
@-rm *.resources 1>/dev/null 2>&1
@-rm -fr $(CULTUREDIRS) 1>/dev/null 2>&1
@-rm meta 1>/dev/null 2>&1
mmm:
cat $(TARGET).cpp
cat AssemblyInfo.cpp
@echo
cat makefile
html:
cat /dev/clipboard | code2html.exe | unix2dos
install: $(TARGET).exe
cp $(TARGET).exe ~/bin
cp $(TARGET).exe.manifest ~/bin