random function

Hi.I want to know what is the problem in this program?
I can't build it for these errors in output:
2.cpp(10): error C2064: term does not evaluate to a function taking 1 arguments
2.cpp(11): error C2059: syntax error : ')'
2.cpp(13): error C2064: term does not evaluate to a function taking 1 arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//This program generetes random number.
#include <cstdlib>
#include<ctime>
#define randomize () (srand(time(0)))
#define random (25)(rand ()%25)
#include<iostream>
using namespace std;
int main()
{
	for(int i=0;i<10;cout<<random(25)<<endl,i++);
	randomize ();
	cout<<"After call randomize function.\n";
	for(int i=0;i<10;cout<<random(25)<<endl,i++);
	return 0;
}

Thanks so much...
#define random (25)(rand ()%25)

This makes no sense.


Though, really, you should avoid using macros anyway. Write an actual function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//  BAD:  don't use #define unless you absolutely have to.  Avoid it!
// #define randomize () (srand(time(0)))
// #define random (25)(rand ()%25)

// BETTER:  write proper functions
void randomize()
{
    srand(time(0));
}

int random(int end)
{
    return rand() % end;
}



EDIT... also... clarity is more important than cramming as much as you can onto one line. This:
1
2
for(int i=0; i<10; i++)
    cout<<random(25)<<endl;


should be preferred over this:
 
for(int i=0;i<10;cout<<random(25)<<endl,i++);


As your intent is much more clear.
Last edited on
Thanks for your answer
I used them in my code, is the way of use it true?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
void randomize()
{
    srand(time(0));
}

int random(int end)
{
    return rand() % end;
}
void gotoxy (int,int);
using namespace std;
int main ()
{
	randomize ();
	char sg=1;
	int x=11,y=31;
	int x1=random(25);
	int y1=random(25);
	cout<<"At first";
	cout.put(sg);
	gotoxy (x,y);
	cout<<"is here but after use random function it is there see:";
	cout.put(sg);
	gotoxy(x1,y1);
	cout<<endl;
	return 0;
}
void gotoxy (int x,int y)
	{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
cout.flush();
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition( hConsoleOutput,dwCursorPosition);

}

When I compile it I take only this warning:
" .cpp(7): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data"
but I can't build it yet:-(
Last edited on
srand() needs an unsigned int as a parameter
but time() gives a time_t

To correct, you should cast the return value to an unsigned int:

 
srand( static_cast<unsigned int>( time(0) ) );
Thanks so much.I get it.
Topic archived. No new replies allowed.