|
-
Jan 8th, 2006, 04:15 PM
#1
[RESOLVED] question: how to generate random numbers
hello,
i know to use srand, rand funcs but they do not generate random numbers.
basically i have 2 questions-
1: does Dev C++ has randomize() and random() functions??
2: do i have to loop again to check if the newly generated random number have been generated before, or is there any function present to do this?
thank you.
-
Jan 8th, 2006, 04:50 PM
#2
Re: question: how to generate random numbers
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
srand(time(NULL));
for(int i= 0; i<10; i ++){
cout<<rand()%10<<endl;
}
getch();
return 0;
}
-
Jan 9th, 2006, 05:36 AM
#3
Re: question: how to generate random numbers
Jmacp,
thank you for your response. i already know this. but have you tried your code?? it may not generate different random numbers. thats what my query is: do i have to store these numbers somewhere (like array) and loop to check if the newly generated number was also generated before.
thank you.
-
Jan 9th, 2006, 06:23 AM
#4
Re: question: how to generate random numbers
rand() has no guarantee that it doesn't create the same number twice or more times in a row. The only guarantee it has is that, over time, the distribution of the numbers it generates is uniform over the range 0 <= n <= RAND_MAX. Doing a modulo 10 on this range will make repeats a lot more likely (and since 10 is neither a prime number nor a power of 2, it will also make the distribution non-uniform, which is when quality numbers are needed, a different method is used.)
But if you want high-quality pseudorandom numbers, you should look into Boost.Random, which is also part of C++ TR1.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2006, 06:32 AM
#5
Re: question: how to generate random numbers
 Originally Posted by CornedBee
rand() has no guarantee that it doesn't create the same number twice or more times in a row. The only guarantee it has is that, over time, the distribution of the numbers it generates is uniform over the range 0 <= n <= RAND_MAX. Doing a modulo 10 on this range will make repeats a lot more likely (and since 10 is neither a prime number nor a power of 2, it will also make the distribution non-uniform, which is when quality numbers are needed, a different method is used.)
But if you want high-quality pseudorandom numbers, you should look into Boost.Random, which is also part of C++ TR1.
thanks, thats look promising.
could you please explain how to use them?
-
Jan 9th, 2006, 06:36 AM
#6
Re: question: how to generate random numbers
Nope, never used them myself, so I don't know. Read the documentation.
But there's a very simple example right at the page I linked.
Code:
boost::mt19937 rng; // produces randomness out of thin air
// see pseudo-random number generators
boost::uniform_int<> six(1,6) // distribution that maps to 1..6
// see random number distributions
boost::variate_generator<boost::mt19937&, boost::uniform_int<> >
die(rng, six); // glues randomness with mapping
int x = die(); // simulate rolling a die
All you need to do is replace the range with whatever range you want (that would be (0,9) in Jmacp's example).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2006, 07:22 AM
#7
Addicted Member
Re: question: how to generate random numbers
 Originally Posted by Jmacp
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
srand(time(NULL));
for(int i= 0; i<10; i ++){
cout<<rand()%10<<endl;
}
getch();
return 0;
}
<conio.h> non-standard header
getch() non-standard function
and you need <ctime> for your code to compile
#Appreciate others by rating good posts !!
#The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.
#People who are still ignorant of their ignorance are dangerous.
-
Jan 10th, 2006, 09:15 AM
#8
Re: question: how to generate random numbers
 Originally Posted by sunnypalsingh
<conio.h> non-standard header
getch() non-standard function
and you need <ctime> for your code to compile
thats 1 headache in Dev C++, it will compile the code even if <ctime> header file is not included!! (atleast Jmacp's code is running on my pc without <ctime>, but of course, not solving my problem)
i wonder how and why??
-
Jan 10th, 2006, 09:24 AM
#9
Re: question: how to generate random numbers
Because standard headers can and do internally include each other. Thus, your particular standard library might include ctime from, say, iostream, and everytime you grab iostream you also get ctime.
The problem is that you can't rely on this behaviour, because other standard libraries might do things differently.
Another issue is that GCC is, due to legacy code still compiled with it, very forgiving regarding not declaring functions. You can call any function that returns int or void without ever declaring it, and GCC will merely emit a warning, and that only if you've got all warnings enabled.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 10th, 2006, 12:18 PM
#10
Re: question: how to generate random numbers
thats what i thought of CB, but just wanted to confirm. thank you.
all right then, i believe that either i have to use files from boost.org (as suggested by CB) or using the standard method of srand with loop checking all the previous generated random numbers.
and still one question left: does Dev C++ has randomize() and random() functions??
thank you.
-
Jan 10th, 2006, 12:21 PM
#11
Re: question: how to generate random numbers
No, I'm pretty sure it doesn't.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 13th, 2006, 02:15 PM
#12
Re: question: how to generate random numbers
 Originally Posted by CornedBee
No, I'm pretty sure it doesn't.
yeah, it seems so.
thanks to all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|