Bitcoin Forum
May 21, 2024, 10:01:24 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: fastest way in C to generate numbers  (Read 294 times)
nomachine
Member
**
Offline Offline

Activity: 274
Merit: 12


View Profile
February 13, 2024, 01:00:26 PM
Last edit: February 13, 2024, 02:07:41 PM by nomachine
 #21

test.cpp
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <gcrypt.h>
#include <gmp.h>
#include <sys/random.h>

gmp_randstate_t state;

void *wrapper_gcry_alloc(size_t size);
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size);
void wrapper_gcry_free(void *ptr, size_t cur_size);

int main() {
    int n;

    printf("Enter the value of n: ");
    scanf("%d", &n);

    mpz_t key;
    mpz_init(key);

    clock_t start_random = clock();

    size_t bytes = (n + 7) / 8; // bytes needed for n bits
    unsigned char *buffer_key = (unsigned char *)malloc(bytes);

    if (buffer_key == NULL) {
        printf("Memory allocation failed.");
        return -1;
    }

    getrandom(buffer_key, bytes, 0); // No flags needed

    mpz_import(key, bytes, 1, 1, 0, 0, buffer_key);

    free(buffer_key);
    mpz_clear(key);

    clock_t end_random = clock();

    double time_random = ((double)(end_random - start_random)) / CLOCKS_PER_SEC;

    printf("Time taken for random method: %f seconds\n", time_random);

    return 0;
}

void *wrapper_gcry_alloc(size_t size) {
    return malloc(size); // Changed to malloc
}

void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size) {
    return realloc(ptr, new_size); // Changed to realloc
}

void wrapper_gcry_free(void *ptr, size_t cur_size) {
    free(ptr); // Changed to free
}


Code:
 g++ -m64 -march=native -mtune=native  -O3 test.cpp -o test $(libgcrypt-config --cflags --libs) -lgcrypt -lgmp 

./test
Enter the value of n: 40
Time taken for random method: 0.000012 seconds

This is a very demanding one.

This code will loop through all numbers from 1 to 2^n - 1   and increment by 1 (optionally print each number)

test.cpp
Code:
#include <sys/random.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <gcrypt.h>
#include <gmp.h>


gmp_randstate_t state;

void *wrapper_gcry_alloc(size_t size);
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size);
void wrapper_gcry_free(void *ptr, size_t cur_size);

int main() {
    int n;

    printf("Enter the value of n: ");
    scanf("%d", &n);

    mpz_t key;
    mpz_init(key);

    clock_t start_random = clock();

    size_t bytes = (n + 7) / 8;
    unsigned char *buffer_key = (unsigned char *)malloc(bytes);

    if (buffer_key == NULL) {
        printf("Memory allocation failed.");
        return -1;
    }

    getrandom(buffer_key, bytes, 0);

    mpz_import(key, bytes, 1, 1, 0, 0, buffer_key);

    free(buffer_key);

    // Loop through all numbers from 1 to 2^n - 1 and add 1 to each number
    mpz_t num;
    mpz_init(num);
    mpz_set_ui(num, 1);

    mpz_t max_num;
    mpz_init(max_num);
    mpz_ui_pow_ui(max_num, 2, n);
    mpz_sub_ui(max_num, max_num, 1);

    while (mpz_cmp(num, max_num) <= 0) {
        // Print the current number (num)
        // gmp_printf("%Zd\n", num);
        // Increment num by 1
        mpz_add_ui(num, num, 1);
    }

    mpz_clear(num);
    mpz_clear(max_num);
    mpz_clear(key);

    clock_t end_random = clock();

    double time_random = ((double)(end_random - start_random)) / CLOCKS_PER_SEC;

    printf("Time taken for random method: %f seconds\n", time_random);

    return 0;
}

void *wrapper_gcry_alloc(size_t size) {
    return malloc(size);
}

void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size) {
    return realloc(ptr, new_size);
}

void wrapper_gcry_free(void *ptr, size_t cur_size) {
    free(ptr);
}

# ./test
Enter the value of n: 30
Time taken for random method: 6.051514 seconds
BlackHatCoiner
Legendary
*
Online Online

Activity: 1526
Merit: 7401


Farewell, Leo


View Profile
February 13, 2024, 08:00:26 PM
 #22

I just tested out the min/max values on my system:
[...]

Result:
Quote
Min long double: 3.3621031431e-4932
Max long double: 1.1897314954e+4932
That's correct. Notice how astronomically small the minimum long double is. If you increment it by epsilon (the smallest possible increment in floating point numbers), it goes from e-4932 to e-19. That's lack of accuracy; tradeoff when working with 64 bits and real numbers.  Wink

Another fun fact. The maximum long double number is 1.1897314954e+4932, as rightly said. This means that long doubles are 2^128 in total, in a range of [~0, ~10^4932]. The more you increase the wanted number, the more inaccurate it becomes.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: « 1 [2]  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!