errori n code blocks random was not declared in this scope

Status
Not open for further replies.

sachin vishnoi

Estimable
Sep 7, 2014
1
0
4,510
when i write this statement in code blocks
randomize();
and
r1=random(991)+10;
the error is coming random not declared in the scope
and
randomize not declared in the scope

 
Solution
the function that you are looking for is rand() not random().

rand() will return a random integral number between 0 and RAND_MAX

If you wish to generate a random number less than RAND_MAX, take the modulus of the returned value and your own max value.

For example,

C++:
int my_random_value = 0;
int my_max_value = 500;
my_random_value = rand() % my_max_value

will generate a random number between 0 and 499. If you wish to normalize this to the range 1 to 500, simply add 1

C++:
my_random_value = (rand() % my_max_value) + 1

Pinhedd

Distinguished
Moderator
the function that you are looking for is rand() not random().

rand() will return a random integral number between 0 and RAND_MAX

If you wish to generate a random number less than RAND_MAX, take the modulus of the returned value and your own max value.

For example,

C++:
int my_random_value = 0;
int my_max_value = 500;
my_random_value = rand() % my_max_value

will generate a random number between 0 and 499. If you wish to normalize this to the range 1 to 500, simply add 1

C++:
my_random_value = (rand() % my_max_value) + 1
 
Solution
CodeBlocks can be used for variery of programming languages. Assuming that you're using it for C/CPP, you might also want to make sure
Code:
#include <stdlib.h>
appears in the first couple of your source file.
Help on rand and random can be obtained on the command line
Code:
man 3 rand
man 3 random
 
Status
Not open for further replies.

TRENDING THREADS