To generate random numbers (the simple way), you should include time.h
You seed the random number generator with srand ( time ( NULL ) );. Only do this once. You can then get a random number between 0 and 32767 with rand ( );. Send it to the console with printf or cout, and put it in an infinite loop to keep getting more.
First of all, there's a difference between pseudo-random numbers and true random numbers. Pseudo-random numbers are easily generated using known algorithms, and they suit the most common purposes. Actual "true" random numbers can only be "generated" with specialized services and/or hardware, and they're used almost exclusively for security and scientific reasons.
Now, why do you need
infinite random numbers
? I guarantee you that you don't. How do I know that? Because infinity isn't real, and there's no way that a user will demand infinite pseudo-random numbers.
What exactly is the number range? Based on your description, the number range would have to be infinite. If you want infinite random numbers, the only way you can guarantee that your numbers are non-repeating is if the number range is infinite - which is an impossibility. I could go on about how you can try to emulate that sort of behavior, but it wouldn't make sense for me to do so, because I think you're asking the wrong questions.
No number range, no repetition... infinite random numbers flowing like infinite loop..
yes for sure, there is no actual need of this program, but only for practice i want to do that
It is not possible within the limited confines of our computers. What you want is only possible within our imaginations, and by the universe(es) itself(themselves).
First of all, the number range has to be known. It also cannot be truly "infinite" because the random numbers you're generating need to be stored somewhere - which implies memory, which isn't infinite. The number range is limited by the amount of memory which is available to represent it, and your ability to be a thrifty programmer.
Secondly, because the number range cannot be infinite, you cannot have non-repeating numbers. If my number range is between 0 - 2 inclusive, then very soon all possible numbers in that range will have been generated. As the range gets bigger, obviously, it becomes less likely you'll stumble upon the same number more than once (this also depends on whether or not the random device is biased in some way).
In your scenario, since there is no defined number range, and you'll never generate the same number twice, you must be able to represent infinitely large numbers in infinitely large data types with your infinitely large RAM, which manifests itself in the real world as an impossibility.
However, I think you're seriously underestimating the ability of the "randomness" that is available to you. You don't need things to be non-repeating and infinite to make good approximations of the real thing.
Thanks xismn.. thank you very much dear, but I've one more question, when we start infinite loop that displays infinite numbers, why doesn't it occupies memory ?
Can not in same way we done infinite non-repeating random numbers ?
An int is typically 4 bytes. The number range you can represent with a signed, 4-byte int, is –2,147,483,648 to 2,147,483,647.
That's four bytes of memory. Not that much. However, the number range I mentioned isn't infinite. It's finite.
Also, take a look at this snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdint>
#include <climits>
int main(int argc, char* argv[]) {
uint8_t number = 255;
//uint8_t is a typedef in cstdint, and it's an unsigned char.
//uint8_t is 8 bits (or one byte), and because it is unsigned it can represent values ranging from 0 to 255
//That's a total of 256 different values it can represent, which makes sense because 2^8 is 256.
std::cout << "Smallest value that a uint8_t can represent:\t" << (int)std::numeric_limits<uint8_t>::min() << std::endl;
std::cout << "Largest value that a uint8_t can represent:\t" << (int)std::numeric_limits<uint8_t>::max() << std::endl;
std::cin.get();
return 0;
}
when we start infinite loop that displays infinite numbers, why doesn't it occupies memory ?
Every time a computer runs a program/application/ect it occupies memory. It can be RAM or secondary memory. In this case, if you were able to create a truly random infinite loop, the program would be storing its sequence into a memory location. Thus, pointing to what xismn was stating regrading the impossible amount of memory needed to stay un-repeating
I see only one way for a program of such nature to be developed (if possible). It would require a program to self image and then store the sequence images into a bank. Every time a new number is added, a new "photo" of the sequence is taken, and before creating a new number, the image is analyzed.
Keep in mind, chance are you would never need such a loop, and if so, best of luck funding it.
Afaik true random numbers can only be generated using a seed from an unpredictable physical phenomenon... Maybe you'll need some quantum mechanics knowledge to do that.
A truly infinite and non-repetitive series of numbers is not only mathematically impossible, but impossible for any computer to do. The reason why is explained in the previous comments. However, a seemingly infinite series of numbers is easily possible. After all, it isn't likely that a random generator with a range of ~4.3 billion will show repetition close enough to each other for the human eye to easily see.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
int main() {
srand(time(NULL));
int a = rand() ;
int b = rand();
int c = rand();
std::cout << "Press up to break"<<std::endl;
Sleep(500);
while (true) {
if (a == b || a == c) {
++a;
}
if (b == c) {
++b;
}
if (GetAsyncKeyState(VK_UP) {
break;
}
std::cout << a <<std::endl;
std::cout << b <<std::endl;
std::cout << c <<std::endl;
}
return 0;
}