I need to make a program that outputs a random number between 1 and 10 every half of a second. Right now i dont know how to generate random numbers or use a timer so i need lots of helf here.

use srand() to seed the random number generator, rand() to get a random number, and the mod operator % to imit the number betwen 1 and 10. Get that working in your program first, then we can talk about how to make it do that every 1/2 second.

You can find examples lof those functions simply by searching this board or google for rand()

ok i got this code to output however many random numbers the user wants between a range of numbers. NOw how to make the numbers output every half second:

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    int num;
    int low;
    int high;
    
    cout << "How many random numbers do you want." << endl;
    cin >> num;
    cout << "What would you like to be the lowest number." << endl;
    cin >> low;
    cout << "What would you like to be the highest number." << endl;
    cin >> high;

    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=low, highest=high; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<num; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
        cout << random_integer; 
    } 

 
    system("PAUSE"); 
}

you need to use the Sleep() function in Windows or sleep() in *nbix. Note the capatilization of the letter S.

while(true)
{
     // display a random number
     Sleep(500); 
}

If you don't want to use that system functin then you could probably create a loop using the clock() function.

you need to use the Sleep() function in Windows or sleep() in *nbix. Note the capatilization of the letter S.

while(true)
{
     // display a random number
     Sleep(500); 
}

If you don't want to use that system functin then you could probably create a loop using the clock() function.

when i ran the program it said that Sleep was an undeclared - do i need an extra header or something

#include <windows.h> If you don't have that header then you will either have to download the Windows Platform SDK, which is pretty large, or write your own loop using clock() function.

#include <windows.h> If you don't have that header then you will either have to download the Windows Platform SDK, which is pretty large, or write your own loop using clock() function.

Nevermind i figured it out

This article has been dead for over six months. Start a new discussion instead.