Home > Blockchain >  How to bind numbers together into an integer without addition?
How to bind numbers together into an integer without addition?

Time:12-09

For example: If I end up with multiple digits and all of those numbers are generated randomly (1-9) up to 7 digits total and I want them all to end up as a whole integer, how do I go by doing this?

Here is something I have tried:

static void generate_key(std::map<std::string, int>& key, std::string c_user)
{
    unsigned int hold[7]{}; // will hold all the random integers seperate to add onto final
    int *final = new int; // will store a complete 7 digit integer here later on

    for (int i = 0; i < 7; i  )
    {
        unsigned int num = rand() % 9;
        hold[i] = num;
    }

    *final = hold[0]   hold[1]   hold[2]   hold[3]; // I know this sums everything but how do I get it to just add all the numbers into a single integer?!
    key.emplace(c_user, final);

    std::cout << "Key created: " << *final << '\n';

    delete final;
}

Hold all integers seperate from eachother:

unsigned int hold[7]{};

Create random digits:

for (int i = 0; i < 7; i  )
        {
            unsigned int num = rand() % 9;
            hold[i] = num;
        }

Store and add all integers into *final integer

int *final = new int;

I want to generate a 7 digit key and each of every single digit is randomized. I then want to add them all up to make a whole integer that gets put into a map where a string has already been created and that key will represent the string. Do you get what I'm saying here? Sorry if my explanation is terrible I'm just too lost here..

Here is the full code if thats helpful:

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stdlib.h>

class Account
{
private:
    std::string username;
    std::string password;
public:
    Account(std::string username, std::string password)
    {
        this->username = username;
        this->password = password;
    }
    ~Account() {};

    std::string get_username()const { return username; }
    std::string get_password()const { return password; }
};

static void create_account(std::string user, std::string pass, std::vector<Account> &acc)
{
    Account *account = new Account(user, pass);

    acc.push_back(*account);

    delete account;
}

static void generate_key(std::map<std::string, int>& key, std::string c_user)
{
    unsigned int hold[7]{};
    int *final = new int;

    for (int i = 0; i < 7; i  )
    {
        unsigned int num = rand() % 9;
        hold[i] = num;
    }

    *final = hold[0]   hold[1]   hold[2]   hold[3];

    key.emplace(c_user, final);

    std::cout << "Key created: " << *final << '\n';

    delete final;
}

std::vector<Account> accounts;
std::map<std::string, int> keys;

int main()
{
    srand(time(NULL));

    create_account("random", "triathlon", accounts);
    generate_key(keys, accounts[0].get_username());

    return 0;
}

static void create_account(std::string, std::string, std::vector<Account> &acc);
static void generate_key(std::map<std::string, int>&, std::string);

CodePudding user response:

If you want to generate a 7 digit number where all of the digits are unique, then you can store the valid digits in a vector and then shuffle that vector and take the first 7 elements as the digit to make the number with. That could be done like:

std::vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
std::random_device rd;
std::mt19937 g(rd());
std::suffle(digits.being(), digits.end(), g);
std::string number{digits.begin(), digits.begin()   7}

and now number is a 7 digit string where each digit only appears once. If you want to not have a leading zero, then you would need to check if the first element is a zero and if so, shuffle again or just takes elements 1 through 7.

If you just want to generate a 7 digit number, but you don't care if there are duplicate digits, then you can do that with a std::string and a simple for loop like:

std::random_device rd;
std::mt19937 g(rd());
std::uniform_int_distribution<> dist(0, 9);
std::string number;
for (int i = 0; i < 7;   i)
{
    number.push_back('0'   dist(g))
}

CodePudding user response:

If you just want a random seven digit number with unique digits you can generate that directly using the typical random shuffle algorithm where you generate a random index into an array take that item, swap the back of the array with it, and pop back of the array.

I forget what this algorithm is called.

In your case you only need to do seven iterations starting with the nine digits. Code below:

#include <vector>
#include <array>
#include <algorithm>
#include <iostream>

int remove_rnd_item( std::vector<int>& digits ) {
    int n = rand() % digits.size(); // should use a better RNG...
    int rnd_digit = digits[n];
    //swap with the last digit and pop...
    std::swap(digits[n], digits.back());
    digits.pop_back();
    return rnd_digit;
}

int random_seven_digit_number_with_unique_digits() {
    std::vector<int> digits = { 0,1,2,3,4,5,6,7,8,9 };
    std::array<int, 7> rnd_digits;
    std::generate(rnd_digits.begin(), rnd_digits.end(),
        [&digits]() {
            return remove_rnd_item(digits);
        }
    );
    // convert the digits vector to a single int, if that is what you want
    int tens_place = 1;
    int val = 0;
    for (auto digit : rnd_digits) {
        val  = tens_place * digit;
        tens_place *= 10;
    }
    return val;
}

int main() {
    for (int i = 0; i < 10;   i) {
        std::cout << random_seven_digit_number_with_unique_digits() << "\n";
    }
}

CodePudding user response:

I assume that the requirement to have all unique digits in the customer ID is erroneous.

To get a random 7-digit number you only need:

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(0, 9'999'999);

    std::cout << distrib(gen) << std::endl;
}

If you want to avoid leading zeroes, use distrib(1'000'000, 9'999'999);

CodePudding user response:

Weeee! Hi!.I'm a beginner too!. and yeah. I think you can do that like this!.

int boop(){
    int ding=1000000;
    int a[7]={1,4,6,3,4,2,4};
    int re=0;
    for(int i=0;i<7;i  ){
        re =a[i]*ding;
        ding/=10;
    }
    return re;
}

or just make your own function to make a random number ranging from 0 to 1. and multiply it with 9999999. (kinda recommended).

#include <iostream>
#include <math.h>
float boop(int seed){
    return abs(sin( cos(seed*3972)*38472));
}
int main(){
    float f=boop(34)*9999999;
    std::cout<<(int)f;
    return 0;
}
  • Related