BigInteger método java para gmp c ++

I want to convert java code in c++

código es

BigInteger value = new BigInteger(125, RandomNumber); 
BigInteger clone = new   BigInteger(value.toByteArray());

How to write this code in cpp using gmp library?

Please anyone help me. Thanks.

preguntado el 31 de julio de 12 a las 11:07

#include<gmp.h> mpz_t value; mpz_init2(randomnumber,125); -

I recommend have a look at the documentation. If you did so already, you describe what problems you are facing with your code. -

gmp_randstate_t state; gmp_randinit_default(state); to initialize the RNG, then mpz_t value; mpz_urandomb(value, state, 125); to generate the random number. Completely untested, I'm just reading the documentation. -

I want a 125 bit random number from current time using gmp. Can you please let me tell how it get? -

Thanks i get 125 bit number from random number. i want random number from current time in milisecond -

2 Respuestas

With C++ you can do that

#include <gmpxx.h>
#include <gmp.h>
#include <iostream>
using namespace std;

int main(){
  mpz_class value;
  mpz_class clone;
  gmp_randclass r(gmp_randinit_default);
  value = r.get_z_bits(125);
  clone = value;
  cout << value << endl;
  cout << clone << endl;                                                                                                                                                                     
  return 0;

and compile with g++ file.cpp -lgmpxx -lgmp

to install libgmpxx.a put --enable-cxx to the build option of ./configure

Respondido 31 Jul 12, 12:07

I am using VC++. I am using gmp.h and gmp.lib file. How i use gmpxx.h? - user1545975

Please help me how i use gmpxx.h in VC++ project - user1545975

Puedes intentar esto:… - Gilead

here is a carbon copy from wikipedia

Here is an example of C code showing the use of the GMP library to multiply and print large numbers:

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

int main(void)
 mpz_t x;
 mpz_t y;
 mpz_t result;


 mpz_set_str(x, "7612058254738945", 10);
 mpz_set_str(y, "9263591128439081", 10);

 mpz_mul(result, x, y);
 gmp_printf("\n    %Zd\n*\n    %Zd\n--------------------\n%Zd\n\n", x, y, result);

 /* free used memory */

This code calculates the value of 7612058254738945 × 9263591128439081.

Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)


Respondido 31 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.