Blum Blum Shub
-
I have to create a random number generator Blum Blum Shub in C or C++ ,using libraries stlib.h and time.h
Then do it!
-
And time.h how to use in it?
-
No idea, because we do not have the instructions that your teacher gave you. If you read the documentation for the random and time functions (time, _time32, _time64 | Microsoft Docs[^]) you should be able to decide how they can help you.
Here's the full indications that i have : Purpose: to develop a function that generates random numbers from a given range. Elaboration of the module for generating random numbers Resources : C or C++ compiler stdlib.h and time.h libraries Algorithm Blum Blum Shub Hope it helps in any kind
-
Here's the full indications that i have : Purpose: to develop a function that generates random numbers from a given range. Elaboration of the module for generating random numbers Resources : C or C++ compiler stdlib.h and time.h libraries Algorithm Blum Blum Shub Hope it helps in any kind
-
Hi everyone . I am a newbie in programming , so i would be very grateful if you could help me . I have to implement the Blum Blum Shub in C or C++ using the stdlib.h and time.h libraries . Can anyone help me with this ? Thanks :)
You are not asking a specific question. You are essentially asking someone to do all the work for you, which will not happen. Get started on it and if you get stuck on something specific then come back and show where you are stuck and someone will be glad to help.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
Hi everyone . I am a newbie in programming , so i would be very grateful if you could help me . I have to implement the Blum Blum Shub in C or C++ using the stdlib.h and time.h libraries . Can anyone help me with this ? Thanks :)
Member 14956475 wrote:
I have to implement the Blum Blum Shub in C or C++...
More importantly, can you do it with pencil and paper? If the answer is no, then: 1) you are going to be hard pressed to implement it in code, and 2) you should work on it until you can produce results on paper. Don't let computer code take the place of the basic understanding of algorithms.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Member 14956475 wrote:
I have to implement the Blum Blum Shub in C or C++...
More importantly, can you do it with pencil and paper? If the answer is no, then: 1) you are going to be hard pressed to implement it in code, and 2) you should work on it until you can produce results on paper. Don't let computer code take the place of the basic understanding of algorithms.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Okay , this is the code that i managed to do :
#include
#include
#include
/*BLUM BLUM SHUB*/int main()
{const int p = 30000000091;
const int q = 40000000003;
int N ;
scanf("%i", &N);
const unsigned long long int s = 1200000003730000000273;
unsigned int i,M,x[N];
time_t t;
srand((unsigned) time(&t));M=p*q;
i=0;
x[0]=s;
for(i=1;i<=N;i++){
x[i]=(x[i-1]*x[i-1])%M;
}
for(i=1;i<=N;i++){
printf(" x[%d] = %d\n",i,x[i]);
}system("PAUSE");
return 0;
}I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)
-
Okay , this is the code that i managed to do :
#include
#include
#include
/*BLUM BLUM SHUB*/int main()
{const int p = 30000000091;
const int q = 40000000003;
int N ;
scanf("%i", &N);
const unsigned long long int s = 1200000003730000000273;
unsigned int i,M,x[N];
time_t t;
srand((unsigned) time(&t));M=p*q;
i=0;
x[0]=s;
for(i=1;i<=N;i++){
x[i]=(x[i-1]*x[i-1])%M;
}
for(i=1;i<=N;i++){
printf(" x[%d] = %d\n",i,x[i]);
}system("PAUSE");
return 0;
}I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)
I found at least one issue with the code: You're using the variable
N
before you initialize it with a value. This is a no no.The difficult we do right away... ...the impossible takes slightly longer.
-
Okay , this is the code that i managed to do :
#include
#include
#include
/*BLUM BLUM SHUB*/int main()
{const int p = 30000000091;
const int q = 40000000003;
int N ;
scanf("%i", &N);
const unsigned long long int s = 1200000003730000000273;
unsigned int i,M,x[N];
time_t t;
srand((unsigned) time(&t));M=p*q;
i=0;
x[0]=s;
for(i=1;i<=N;i++){
x[i]=(x[i-1]*x[i-1])%M;
}
for(i=1;i<=N;i++){
printf(" x[%d] = %d\n",i,x[i]);
}system("PAUSE");
return 0;
}I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)
Member 14956475 wrote:
...i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit .
Change your
main()
signature to:void main( int argc, char *argv[] )
Then access
argv[1]
to getD
as a command line argument. Also, your twofor()
loops are accessing beyond the end of thex
array. In C, arrays start at0
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Member 14956475 wrote:
...i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit .
Change your
main()
signature to:void main( int argc, char *argv[] )
Then access
argv[1]
to getD
as a command line argument. Also, your twofor()
loops are accessing beyond the end of thex
array. In C, arrays start at0
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I've replaced main() with
void main( int argc, char *argv[] )
, but how can i acces argv[1] to get D as a command line argument?
-
I've replaced main() with
void main( int argc, char *argv[] )
, but how can i acces argv[1] to get D as a command line argument?
Member 14956475 wrote:
...but how can i acces argv[1] to get D as a command line argument?
The same way you would access parameters from within any other function.
main()
is no different. See here for more.Member 14956475 wrote:
...is it possible to use srand command?
Your code is already doing that. However, the way in which you are using it is meaningless since you are also not calling
rand()
.rand()
by itself will always generate the same sequence of numbers.srand()
will "seed" the number generation process to start at a different spot."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Member 14956475 wrote:
...but how can i acces argv[1] to get D as a command line argument?
The same way you would access parameters from within any other function.
main()
is no different. See here for more.Member 14956475 wrote:
...is it possible to use srand command?
Your code is already doing that. However, the way in which you are using it is meaningless since you are also not calling
rand()
.rand()
by itself will always generate the same sequence of numbers.srand()
will "seed" the number generation process to start at a different spot."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
#include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("enter the number:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); } system("PAUSE"); return 0; } I added srand and rand is it okay working now ?
-
#include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("enter the number:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); } system("PAUSE"); return 0; } I added srand and rand is it okay working now ?
Member 14956475 wrote:
is it okay working now ?
Only you can determine that.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Hi everyone . I am a newbie in programming , so i would be very grateful if you could help me . I have to implement the Blum Blum Shub in C or C++ using the stdlib.h and time.h libraries . Can anyone help me with this ? Thanks :)
Okay , so this is the final code that i have at the moment #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("Introdu numarul de cifre aleatorii:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; FILE *fp; //txt code fp = fopen("file.txt", "w"); //txt code for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); fprintf(fp, "%d ",x[i]); //txt code } system("PAUSE"); fclose(fp);//txt code return 0; } It's working but i have one issue that i can't still solve , i need to put a maximal limit of the generated numbers at input , can anyone give me a clue how i can set x[i] a limit? I only found how to put a limit at rand but when it multiplies with function of BBS it exceeds the limit
-
Okay , so this is the final code that i have at the moment #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("Introdu numarul de cifre aleatorii:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; FILE *fp; //txt code fp = fopen("file.txt", "w"); //txt code for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); fprintf(fp, "%d ",x[i]); //txt code } system("PAUSE"); fclose(fp);//txt code return 0; } It's working but i have one issue that i can't still solve , i need to put a maximal limit of the generated numbers at input , can anyone give me a clue how i can set x[i] a limit? I only found how to put a limit at rand but when it multiplies with function of BBS it exceeds the limit