Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mpz_urandomb problem

122 views
Skip to first unread message

carmen cojocaru

unread,
Nov 26, 2011, 8:13:48 AM11/26/11
to
Hi,
I am trying to generate a sequence of random numbers and I need them
to have the same length(100 bits, not 99, not 98).
But my code doesn't do what I was expecting to do. Can someone tell me
where's the problem?
Thanks in advance.
Regards.

Code:
#include <stdio.h>
#include <time.h>
#include "mpir.h"
int main()
{
int i;
mpz_t number;
gmp_randstate_t rs;
time_t seed;

FILE *f;

time(&(seed));
mpz_init(number);

gmp_randinit_default(rs);

gmp_randseed_ui(rs, (long)seed);


for(i = 0; i < 5; n++) {

mpz_urandomb(number, rs, 100);
mpz_out_str(f, 2, number);

printf("\n");
}
return 0;
}
Output:

110110101010101010100001111001100101101110111100011001000110010001100110110011110100000011010011100
1111000111100110111000111100100011110100000110110010111111001000001101111000101110000011110000100101
1010010101110010010111111000100111100111111110110110011101011011000000110111111101011110111000100110
1111010111111110111011011001100110101011110110100110001011100100000010111110001110010001001101011
11110110101101111111011100100111000011011111110100101011011101010111110001001111001100110001110001

Andrew Haley

unread,
Nov 26, 2011, 9:39:09 AM11/26/11
to
carmen cojocaru <carmenn....@yahoo.com> wrote:
> Hi,
> I am trying to generate a sequence of random numbers and I need them
> to have the same length(100 bits, not 99, not 98).
> But my code doesn't do what I was expecting to do. Can someone tell me
> where's the problem?

I don't think there is one. All of the numbers are 100 bits long, but
the output conversion routine doesn't print the leftmost zeroes.

Andrew.

carmen cojocaru

unread,
Nov 26, 2011, 11:30:03 AM11/26/11
to
On Nov 26, 4:39 pm, Andrew Haley <andre...@littlepinkcloud.invalid>
wrote:
> carmen cojocaru <carmenn.cojoc...@yahoo.com> wrote:
> > Hi,
> > I am trying to generate a sequence of random numbers and I need them
> > to have the same length(100 bits, not 99, not 98).
> > But my code doesn't do what I was expecting to do. Can someone tell me
> > where's the problem?
>
> I don't think there is one.  All of the numbers are 100 bits long, but
> the output conversion routine doesn't print the leftmost zeroes.
>
> Andrew.

My apologies. I forgot about this case. Let me give you a more
appropriate output.

101010011000011001001110010111001010000101111111100011111010001101011111110110000100100111110010100
1110010100111111011101011000111101110000011001101111111011110110110101001000110000111001111011010110
1110000100011011010000001010111011101100000011110000001110111101110010101011111010110001000001100110
1001111001000110010101011010000011110000100100010000011100000111100111011100011001100001011010101
1111010101111100010100001110010010101101010100100110111101111110111100111100001101110000110010100

Taking a good look at the last string, it's obvious that the three
leftmost bits are zeroes, so why printing two zero-bits and not all
five bits since they are all zeroes?
Is this also an output conversion?
I'll be waiting for your reply. Thanks.
Carmen.

Andrew Haley

unread,
Nov 27, 2011, 5:25:00 AM11/27/11
to
carmen cojocaru <carmenn....@yahoo.com> wrote:
> On Nov 26, 4:39?pm, Andrew Haley <andre...@littlepinkcloud.invalid>
> wrote:
>> carmen cojocaru <carmenn.cojoc...@yahoo.com> wrote:
>> > Hi,
>> > I am trying to generate a sequence of random numbers and I need them
>> > to have the same length(100 bits, not 99, not 98).
>> > But my code doesn't do what I was expecting to do. Can someone tell me
>> > where's the problem?
>>
>> I don't think there is one. ?All of the numbers are 100 bits long, but
>> the output conversion routine doesn't print the leftmost zeroes.
>
> My apologies. I forgot about this case. Let me give you a more
> appropriate output.
>
> 101010011000011001001110010111001010000101111111100011111010001101011111110110000100100111110010100
> 1110010100111111011101011000111101110000011001101111111011110110110101001000110000111001111011010110
> 1110000100011011010000001010111011101100000011110000001110111101110010101011111010110001000001100110
> 1001111001000110010101011010000011110000100100010000011100000111100111011100011001100001011010101
> 1111010101111100010100001110010010101101010100100110111101111110111100111100001101110000110010100
>
> Taking a good look at the last string, it's obvious that the three
> leftmost bits are zeroes, so why printing two zero-bits and not all
> five bits since they are all zeroes?

I don't understand what you're asking.

Andrew.
0 new messages