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;
}

Recommended Answers

All 2 Replies

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;

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.