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

Hi there, I'm just getting back into c++ after a 2 year break.
I'm just wondering how I go about using the numbers generated here later on in the program and is there also a way to get I to generate numbers with decimal values as well.

Edited by pyTony: fixed formating

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by WaltP
0

>>how I go about using the numbers generated here later on in the program
1. use global variables (not recommended)

2. make the variables part of a c++ class

3. pass the variables around as parameters to the function(s) that need them.

>>way to get I to generate numbers with decimal values as well
make the variables of type float then divide the value returned by rand() by 100 to get two decimal places.

0

Hi there, I'm just getting back into c++ after a 2 year break.

You are also hijacking a thread by someone else. Start a new thread with your question, and use CODE tags, not INLINECODE tags for a block of code.

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.