| | |
program to generate random numbers for a given range
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdlib.h> int main() { int i; cout<<"ten random numbers for the range 0 to 50"<<endl; for(i=0;i<10;i++) { cout<<rand()<<endl; return 0; } }
Last edited by Ancient Dragon; Mar 30th, 2008 at 7:37 am. Reason: add code tags
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
•
•
•
•
i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?
#include<iostream.h>
#include<stdlib.h>
int main()
{
int i;
cout<<"ten random numbers for the range 0 to 50"<<endl;
for(i=0;i<10;i++)
{
cout<<rand()<<endl;
return 0;
}
}
C++ Syntax (Toggle Plain Text)
ten random numbers for the range 0 to 50 41 18467 6334 26500 19169 15724 11478 29358 26962 24464
Last edited by VernonDozier; Mar 30th, 2008 at 4:15 am. Reason: Edited grammar to make post more clear.
Well i have seen your code. It actually can create random numbers from the whole integer set.
Therefore i have just edited the whole program and made it such that it will get random numbers from 0 to 50.
Please dont copy the code but, Check out how the loop actually is running and implement it on your own.
Hope your problem is solved with this.
However there is another problem . Some times the random numbers may be the same. I mean there can be two 28 or any other number coming out. Try avoiding it by using an array.
Therefore i have just edited the whole program and made it such that it will get random numbers from 0 to 50.
Please dont copy the code but, Check out how the loop actually is running and implement it on your own.
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int randomnumber(); int main() { int i; cout<<"ten random numbers for the range 0 to 50"<<endl; for(i=0;i<10;i++) { randomnumber(); } cin.get(); } int randomnumber() { int j; j = rand(); if (!(j >= 50)) { if (!(j<=0)) { cout<< j<<"\n"; } } else {randomnumber();} }
Hope your problem is solved with this.
However there is another problem . Some times the random numbers may be the same. I mean there can be two 28 or any other number coming out. Try avoiding it by using an array.
So instead of Recursion i should go on getting the remainder by dividing the number with some value like 50 or so?
I usually initialize by using srand() function
time() is in the header <ctime> or <time.h> i am not very sure.
To generate random numbers from 0-50 use the % operator
c++ Syntax (Toggle Plain Text)
srand ( time(NULL) );
time() is in the header <ctime> or <time.h> i am not very sure.
To generate random numbers from 0-50 use the % operator
C++ Syntax (Toggle Plain Text)
int random_numer= rand()%50;
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
The function rand() produced integers between 0 and a large (system-dependent) integer called RAND_MAX. To get integers between 0 and 50, write:
C++ Syntax (Toggle Plain Text)
cout << 50*rand()/RAND_MAX << endl;
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
C++ Syntax (Toggle Plain Text)
int number = rand() % N; // gives you a random number from 0..(N-1) number += L; // the number between L..(L+N-1)
![]() |
Similar Threads
- C++ Random Number Generator (C++)
- Hello I need help on a random number generator. PLEASE (C++)
- C++ Random Numbers (C++)
- how to use generate random numbers (Visual Basic 4 / 5 / 6)
- Generating random numbers using VC6 (C++)
- Program will not generate random numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: HELP!! how do you use atoi?
- Next Thread: is c++ hard?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






