Aug 6, 2009 at 5:01pm Aug 6, 2009 at 5:01pm UTC
Hello everyone!
I don't know if it's a right plase to ask about visual C++, but anyway...
I need to generate some random integers. Usualy in C++ I use srand() and rand(), from #include<ctime> methods, but they don't work in visual studio 2008. So maybe someone can help me?
Aug 6, 2009 at 5:06pm Aug 6, 2009 at 5:06pm UTC
Why you say they don't work? Which errors / results do you get?
Which project type did you create?
Aug 7, 2009 at 2:27am Aug 7, 2009 at 2:27am UTC
Oh John, please take out the stdafx crap. :)
Aug 9, 2009 at 1:30pm Aug 9, 2009 at 1:30pm UTC
I've created CLR->Widows Forms Application project, and I need to generate those random numbers in Form1.h....
error C3861: 'srand': identifier not found
error C3861: 'rand': identifier not found
Last edited on Aug 9, 2009 at 1:31pm Aug 9, 2009 at 1:31pm UTC
Aug 9, 2009 at 1:57pm Aug 9, 2009 at 1:57pm UTC
error C2661: 'srand' : no overloaded function takes 0 arguments
Now I get this error. I tryed srand((unsigned ) time(NULL));
and srand(time(0));
Last edited on Aug 9, 2009 at 2:02pm Aug 9, 2009 at 2:02pm UTC
Aug 9, 2009 at 2:16pm Aug 9, 2009 at 2:16pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#pragma once
#include <ctime>
#include <cstdlib>
namespace TSP {
....
private : System::Void randomGraph_Click(System::Object^ sender, System::EventArgs^ e) {
srand((unsigned ) time(NULL));
Form1::n = System::Convert::ToInt32(this ->count->Text);
int x = (rand()%100)+1;
int y = (rand()%100)+1;
Form1::time->Text = x.ToString();
Form1::lenth->Text = y.ToString();
}
};
}
This project is just to get rand work....
Last edited on Aug 9, 2009 at 2:19pm Aug 9, 2009 at 2:19pm UTC
Aug 9, 2009 at 3:02pm Aug 9, 2009 at 3:02pm UTC
eep.. you never said you were using CLI
Afraid I can't help you with that.
Aug 9, 2009 at 4:01pm Aug 9, 2009 at 4:01pm UTC
C++/CLI
1 2 3 4
Random^ randObj= gcnew Random; //Random object with an auto-generated seed
for ( int j = 0; j < 6; j++ )
Console::Write( " {0,10} " , randObj->Next() );
Console::WriteLine();
So your code should be somthing like:
1 2 3 4 5 6 7 8 9 10 11 12 13
private : System::Void randomGraph_Click(System::Object^ sender, System::EventArgs^ e)
{
Random^ randObj= gcnew Random; //Random object with an auto-generated seed
Form1::n = System::Convert::ToInt32(this ->count->Text);
int x = randObj->Next(1, 100);
int y = randObj->Next(1, 100);
Form1::time->Text = x.ToString();
Form1::lenth->Text = y.ToString();
}
NB: untested code but should work.
Last edited on Aug 9, 2009 at 4:37pm Aug 9, 2009 at 4:37pm UTC
Aug 10, 2009 at 4:00pm Aug 10, 2009 at 4:00pm UTC
Thanks for help, this code works.