random numbers with visual c++

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?
Why you say they don't work? Which errors / results do you get?
Which project type did you create?
i think you aren't telling us everything... you have made me curious, so i made a little test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//console app, VC++ 2008
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	srand((unsigned) time(NULL));
	int numbers[10];
	for(int i=0;i<10;i++)
	{
		numbers[i] = rand()%100;
		cout<<numbers[i]<<endl;
	}
	return 0;
}


this works fine by me...
maybe post your code ... or you are working with winForms :)?
Oh John, please take out the stdafx crap. :)
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
#include <cstdlib>
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
nothing is wrong with those lines you posted. At least not that I can see.

This code works:

1
2
3
4
5
6
7
8
9
10
#include <cstdlib> // for srand, rand
#include <ctime>   // for time

int main()
{
    srand( time(0) );
    int foo = rand();

    return 0;
}


Compare what you're doing to make sure it matches that. If you're still having problems, try to start a new project that does nothing but gets rand to work. That way you have a better chance of finding out where the problem is.

If you're still having problems you'll need to post more code.
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
eep.. you never said you were using CLI

Afraid I can't help you with that.
closed account (z05DSL3A)
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
Thanks for help, this code works.
Topic archived. No new replies allowed.