I would like to know that is it possible to generate a list of 16 digit random numbers in C or C++, if it is, then how…..
:confused:
Printable View
I would like to know that is it possible to generate a list of 16 digit random numbers in C or C++, if it is, then how…..
:confused:
Sure its possible, I'll provide a program to do it, but since I don't have a compiler to test it, it may need a bit tweaking here and there.
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;
cout<<"How many outputs would you like?">>endl;
cin>>x;
a = 1 + rand() % 9;
b = 1 + rand() % 9;
cC = 1 + rand() % 9;
d = 1 + rand() % 9;
e = 1 + rand() % 9;
f = 1 + rand() % 9;
g = 1 + rand() % 9;
h = 1 + rand() % 9;
i = 1 + rand() % 9;
j = 1 + rand() % 9;
k = 1 + rand() % 9;
l = 1 + rand() % 9;
m = 1 + rand() % 9;
n = 1 + rand() % 9;
o = 1 + rand() % 9;
p = 1 + rand() % 9;
for(int c = 0; c <= x; c++)
cout<<a<<b<<cC<<d<<e<<f<<g<h<<i<<j<<k<<l<<m<<n<<o<<p<<endl;
}
That may not be pretty, but it should work.
Someone be kind enough to compile and try that. Try not to rip my code apart too much. :P
Well, it seems after some testing from vicTT, its pretty buggy, I'm gunna write it using an array and repost it in this post when finished. heh
Hey guys, I compiled it and fixed a few VERY minor errors. This should work.
include<iostream.h>
#include<stdlib.h>
void main(void)
{
int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;
cout << "How many outputs would you like?"<< endl;
cin>>x;
a = 1 + rand() % 9;
b = 1 + rand() % 9;
cC = 1 + rand() % 9;
d = 1 + rand() % 9;
e = 1 + rand() % 9;
f = 1 + rand() % 9;
g = 1 + rand() % 9;
h = 1 + rand() % 9;
i = 1 + rand() % 9;
j = 1 + rand() % 9;
k = 1 + rand() % 9;
l = 1 + rand() % 9;
m = 1 + rand() % 9;
n = 1 + rand() % 9;
o = 1 + rand() % 9;
p = 1 + rand() % 9;
for(int c = 0; c <= x; c++)
cout << a << b << cC << d << e << f << g<< h << i << j << k << l << m << n << o << p << endl;
}
Umm, specificly 16 digits. Lets see:
In C/C++, you can use the rand() or the srand() functions. They won't give you 16 digit random numbers, you'll have to give it any algorithm you like.. for example:
randomnumber1 = srand() * 91234567890;
randomnumber2 = srand() * 99234567890
.
.
.
Basically, all you're doing here is generating a random number, using the 'srand()' function, and then multiplying it with a sufficiently large number. The number, ofcourse, could be anything at all. Just make sure its big. Real big.
That should be one way of doing it. There might be better ways though. heh. ( for example, xmadness's way. His loop for printing the list is fine, so use that. Just replace the 1 + rand... part with the line I put above. Try both methods, and let me know if 1 + rand() % 9 gives such a big number. :) )
As an afterthought, can a 16 digit number be defined as an integer? I mean, won't it need more space than is reserved for the 'int' type? Just wondering.
And, on a totally unrelated note, a variable in C/C++ can have a really really huge name. I actually created a variable with over 1000 characters, and the complier ( in this case Borland C++ 5 ) gave me no problem. heh. Just something to share. :)
[pong]Cheers.[/pong]
Ah hell, if that code works that sweet. Use that. :)
Thanks, mathgirl, for clearing that up.
Well...testing doesn't give quite the right output.
Quote:
Originally posted here by RejectKnowledge
[b]In C/C++, you can use the rand() or the srand() functions. They won't give you 16 digit random numbers, you'll have to give it any algorithm you like.. for example:
randomnumber1 = srand() * 91234567890;
randomnumber2 = srand() * 99234567890
.
.
.
Yeah your right, using the rand() will always produce the same output each time the program is run. To prevent that you can use the srand(), which requires a pre inputted number to run the algorithm. That way it has a different starting position in the algorithm. Its called a seed value.
Well they way I wrote it, there is only a single value, 0-9, that will be stored in each variable, so in essence, I could have, and should have, use a short variable type. And I prolly should have used an array also.Quote:
As an afterthought, can a 16 digit number be defined as an integer? I mean, won't it need more space than is reserved for the 'int' type? Just wondering.
short num[16];
Heh, gotta love programming, 1000 different ways to do the same thing.
My piece of code will generate any amount of big numbers, because of the multiplication. madness on the other hand is printing out 16 individual random integers. Therefore the loop for using my code will be more alng the lines of:
cout << "How many outputs would you like?"<< endl;
cin>>x;
for ( int c = 0; c < x; c++){
randomnumber = srand() * 99234567890;
cout << randomnumber << endl;
}
There. hehe.
:)
Yep. The beauty of programming. Did you check out the thread about the completition of freaky coding? heh. It's a riot. And yes, using an array would've been more efficient. :)
and mathgirl, did you test my code, or is madness' not giving the right output?
haha, so what is the correct program? Heh, see what happens when you don't have a compiler in front of you?
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;
cout<<"How many outputs would you like?"<<endl;
cin>>x;
for(int c = 0; c < x; c++)
{
a = 1 + rand() % 9;
b = 1 + rand() % 9;
cC = 1 + rand() % 9;
d = 1 + rand() % 9;
e = 1 + rand() % 9;
f = 1 + rand() % 9;
g = 1 + rand() % 9;
h = 1 + rand() % 9;
i = 1 + rand() % 9;
j = 1 + rand() % 9;
k = 1 + rand() % 9;
l = 1 + rand() % 9;
m = 1 + rand() % 9;
n = 1 + rand() % 9;
o = 1 + rand() % 9;
p = 1 + rand() % 9;
cout<<a<<b<<cC<<d<<e<<f<<g<<h<<i<<j<<k<<l<<m<<n<<o<<p<<endl;
}
cout<<"I have returned "<<x<<" results."<<endl;
}
Okay, that should work.. I think. LOL
!!! This is all so exciting! But I have to go work on "my" programming, now. I know we can all go out and cheat by getting free code for a random number generator off the net, but that wouldn't be very much fun, now, would it??? I started revising what you had, rejeckknowledge, but after I fixed a few things...added some libraries, etc. it was getting a little more involved than I have time for at the moment. I'll check back later tonight and see what's up.
Good luck.
Hmm, now my next question. What is this for? If I'm mistaken, It would be a credit card number generator. Tell me thats not what its used for...
Erm. The thought did cross my mind, but I managed to discard it. But yeah, please tell me this isn't for a Credit Card generator, because that would suck.
mathgirl, I didn't add any libraries, because my code wasn't really a complete working program. Twas just a snippet of the loop to generate the number. :)
Cheers.
I would like to Thank all of You to help me out,
Let me take this oppourtinity to inform you that you should be rest assured that this C program is not being used for any credit cards number, or any oter antisocial activity.
As a matter of fact, i donot have the credit card and i do not even know hoemany digit number is there on the credit card.
once again THANX
Unfortunately, this solution isn't statistically viable:Quote:
Originally posted here by RejectKnowledge
Umm, specificly 16 digits. Lets see:
In C/C++, you can use the rand() or the srand() functions. They won't give you 16 digit random numbers, you'll have to give it any algorithm you like.. for example:
randomnumber1 = srand() * 91234567890;
randomnumber2 = srand() * 99234567890
Basically, all you're doing here is generating a random number, using the 'srand()' function, and then multiplying it with a sufficiently large number. The number, ofcourse, could be anything at all. Just make sure its big. Real big.
Multiplying with a constant will limit the range of possible result; for example using 91234567890 or 99234567890 means you will always get a "pseudo-random" number divisible by 10 (or seen in a diffrent way, that ends with 0). In other words, the resulting "pseudo-random" number will always be a multiple of the prime factors of the constant.
A way around this would be to multiply 2 pseudo-random numbers together. This would already be better although I beleive that would also be statistically biased:
Half natural numbers are odd, half are even.
When multiplicating, the only way to obtain an odd number is to multiply two odd numbers.
So, the odds of obtaining an odd number when multipling two random numbers (each with 50% chance of returning an odd number) would yeild only a 25% chance, while it should be 50%...
This might not mean much at first look, but it can be crucial when carefully observed: just take a look at TCP ISN (Initial Sequence Number) prediction for example:
http://razor.bindview.com/publish/papers/tcpseq.html
http://lcamtuf.coredump.cx/newtcp/
Ammo
Here is the completed program. You'll need to run it in a dos window.
And by the way, in order for it to be proper C++, the code should be:
#include<iostream>
#include<cstdlib>
int main()
{
int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;
std::cout<<"How many outputs would you like?"<<std::endl;
std::cin>>x;
a = 1 + rand() % 9;
b = 1 + rand() % 9;
cC = 1 + rand() % 9;
d = 1 + rand() % 9;
e = 1 + rand() % 9;
f = 1 + rand() % 9;
g = 1 + rand() % 9;
h = 1 + rand() % 9;
i = 1 + rand() % 9;
j = 1 + rand() % 9;
k = 1 + rand() % 9;
l = 1 + rand() % 9;
m = 1 + rand() % 9;
n = 1 + rand() % 9;
o = 1 + rand() % 9;
p = 1 + rand() % 9;
for(int c = 0; c <= x; c++)
std::cout<<a<<b<<cC<<d<<e<<f<<g<h<<i<<j<<k<<l<<m<<n<<o<<p<<std::endl;
return 0;
}
just use:
"using namespace std;"
insead of having to define scope each time...
Ammo
The best way is to use the srand() method in main, but initialize it with the time:
srand(time(0)). It will use current time to randomize the use of the rand() function throughout the rest of your program. Happy coding!!
Haha , nice post xmaddness ... i think that yeah , i could be :)??
Good point but it's also worth noting that rand and srand are both inherently *unsafe* for generating random numbers for things such as crypto keys ie. anything that you don't want someone to be able to guess. These two functions both use a very predictable method of picking a seed to generate the random number. It is better to generate random numbers using a large number of different variables that make the end result hard to guess. For example you could write a function to generate random numbers based on lots of different values that change frequently and unpredictably such as: current pid * number of currently open files * bytes memory free * thread group id ....Quote:
Unfortunately, this solution isn't statistically viable:
Multiplying with a constant will limit the range of possible result; for example using 91234567890 or 99234567890 means you will always get a "pseudo-random" number divisible by 10 (or seen in a diffrent way, that ends with 0). In other words, the resulting "pseudo-random" number will always be a multiple of the prime factors of the constant.
A way around this would be to multiply 2 pseudo-random numbers together. This would already be better although I beleive that would also be statistically biased:
Half natural numbers are odd, half are even.
When multiplicating, the only way to obtain an odd number is to multiply two odd numbers.
So, the odds of obtaining an odd number when multipling two random numbers (each with 50% chance of returning an odd number) would yeild only a 25% chance, while it should be 50%...
This might not mean much at first look, but it can be crucial when carefully observed: just take a look at TCP ISN (Initial Sequence Number) prediction for example:
Anyone know a good website that could teach me about C++
I am very interested in it and planing to take programing next year so i just want to be ahead a bit
netcrashxx, i have posted a great site earlier ... it has a lot of Programming Tutorilas ..you might want to check it out :)
http://www.antionline.com/showthread...hreadid=243872
Hey Thank You Memory for the link
i think 50K websites will be enough for a lifetime or two...