First, let me say I am NOT asking for someone to write this program for me. I have to see how functions and operations are used in programs to learn to use them in my own program. And I need help understanding how to use rand() in the program I have to write. I have to write a program that is basically flash cards on the computer. It will distribute random numbers and ask the user to multiply and enter the product. I have to use 2 for loops (one is nested ) to do this. Can anyone tell me a site where I can find a better explination of rand than my text book has?

Recommended Answers

All 3 Replies

You might try Google.

'rand' returns an int value in some range. On MSVC it returns a number between 0 and 0x7FFF (32767).

Each time you call it, it returns a different number in a sequence that is pseudo-random. However, unless you use 'srand' to SEED the random sequence, it will always return the same sequence of numbers.

This can actually be a benefit, because you can write and test your program with rand and get the exact same sequence each time. Once you are happy with your program, introduce 'srand' to seed the number.

As a test, try this:

for (int i = 0; i < 100; i++) printf( "%d\n", rand() );

srand() takes an arg that is the seed value, and possible seeds are things like time:

srand( time(NULL) );

Internally the number is generated this way under MSVC, so you can see it is both simple and repeatable:

return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);

There are much more 'random' random number generators used in encryption, but this should do for now!

This is from the Borland site:

rand() uses a multiplicative congruential random number generator with period 2 to the 32nd power to return successive pseudorandom numbers in the range from 0 to RAND_MAX. The symbolic constant RAND_MAX is defined in stdlib.h

Typical example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>    // time()

int main(void)
{
    int i;

    // randomize();  // Borland
    srand(time(NULL));  // other compilers
    printf("Ten random numbers from 0 to 99\n\n");
    for(i=0; i<10; i++)
       printf("%d\n", rand() % 100);
    return 0;
}

you can also control the min and max numbers the rand will use by using the format avariable = rand() % (highnumber) + (lownumber); (highnumber and lownumber don't have to be variables you may use integer values you actually want instead) here is a simple program that roles dice as an example.

#pragma hdrstop
#include <condefs.h>
#include <iostream.h>
#include <conio.h>


//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char **argv)
{
int dicerole1;
int dicerole2;
cout << "this program will role 2 dice for 2 people randomly getting the numbers." << endl << endl;
dicerole1 = rand() % 6 + 1;
dicerole2 = rand() % 6 + 1;
cout << "my role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl;
dicerole1 = rand() % 6 + 1;
dicerole2 = rand() % 6 + 1;
cout << "your role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl;
getch();
        return 0;
}
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.