0

Hi , im trying to create a simple code that generate random numbers
Im having two problem ,
1. For some reason randomize() function does not work on VisualC++6
2. I didnt really got the idea on how to set the range of the numbers that will be generated using the rand() function ,. i just saw it in some example .

Here is the code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
 int i;
 randomize();
 for (i=0;i<10;i++)
  printf ("Number %d: %d\n",i+1,rand()%10+1);
 return 0;
}
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by WaltP
0

hey,
i will like you to try this for C, i use VC6 but the program is C++ and i get my random numbers anytime i want....

first include this

#include <cstdlib>
using std::rand;

after that.. whenever you want to generate the random numbers,

rand()%4

wil generate one of 0, 1, 2, 3;

now if you want the number to be in the range 1 to 4, just use the code

1 + rand()%4;

now you will get number in the range 1,2,3,4;

note 1 is the smallest and 4 is the largest;

if you want to generate the numbers 2, 4, 6, 8...
its by doing the same but adding 2 instead

2 + rand()%4;
0

1. For some reason randomize() function does not work on VisualC++6

That's because this is C++, not Basic. You 'randomize' using srand(time()); once at the start of the program. Be sure to include time.h

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.