Help with random

Write your question here.

Hi I'm getting error messages with my implementation of the random generator. The error message is written in comments beside the random generator. Can anyone help me check what is the problem? I'm tasked to generate 2 sets of vectors and compare if they are the same (have the same numbers, duplicates are ignored).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <random>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cstdlib>

using namespace std;

bool same_set(vector<int>, vector<int>);


int main()

{
    //random generator
    mt19937 rand_generator;
    rand_generator.seed(time(0));
    uniform_real_distribution<int>rand_distribution(3,7);
    uniform_real_distribution<int>rand_distribution2(1,10);
    vector<int> A, B;
    
    int len;
    len=rand_distribution(rand_generator); //ERROR: Thread 1:EXC_ARITHMETIC...
    
    //generating 2 sets of vectors with length between 3 and 7 and elements between 1 and 10.
    for (int i=1; i<=len; i++) {
        A.push_back(rand_distribution2(rand_generator)); //ERROR: Thread 1:EXC_ARITHMETIC ( I got this when I set len=[a constant];
    }
    len=rand_distribution(rand_generator);
    for (int i=1; i<=len; i++) {
        B.push_back(rand_distribution2(rand_generator));
    }
    
    cout<<same_set(A, B);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//function
bool same_set(vector<int> setA, vector<int> setB) {
    
    //Sorting SetA
    bool swapped=true;
    while (swapped==true) {
        swapped=false;
        for (int i=0; i<setA.size();i++) {
            if (setA[i]>setA[i+1]) {
                swap(setA[i],setA[i+1]);
                swapped = true;
            }
        }
    }
    
    //Sort setB
    swapped=true;
    while (swapped==true) {
        swapped=false;
        for (int i=0; i<setB.size();i++) {
            if (setB[i]>setB[i+1]) {
                swap(setB[i],setB[i+1]);
                swapped = true;
            }
        }
        
    }
    
    //Delete duplicates in SetA
    bool deleted=true;
    while (deleted == true) {
        deleted = false;
        for (long j=setA.size()-1; j>=0;j--) {
            if (setA[j]==setA[j-1]) {
                setA.erase(setA.end());
                deleted = true;
            }
        }
        deleted =true;
    }
    
    //Delete duplicates in SetB
    while (deleted == true) {
        deleted = false;
        for (long j=setA.size()-1; j>=0;j--) {
            if (setA[j]==setA[j-1]) {
                setA.erase(setA.end());
                deleted = true;
            }
        }
    }
    
    //Check if both sets have the same size. If they don't: false.
    if (setA.size()!=setB.size()) {
        return false;
    }
    
    
    //Check if they match term for term. If they dont, false.
    for (int i=0; i<setA.size(); i++) {
        if (setA[i]!=setB[i]) {
            return false;
        }
    }
    
    
    return true;
}


Thanks for your help!
Lines 22, 23

1
2
3
4
5
// uniform_real_distribution<int>rand_distribution(3,7);
uniform_int_distribution<int> rand_distribution(3,7);

// uniform_real_distribution<int>rand_distribution2(1,10);
uniform_int_distribution<int> rand_distribution2(1,10);


std::uniform_real_distribution<> requires a floating point type as the template argument
Thanks for the help JLBorges!

I have made these changes
1
2
3
4
5
    //uniform_real_distribution<int>rand_distribution(3,7);
   // uniform_real_distribution<int>rand_distribution2(1,10);

    uniform_real_distribution<double>rand_distribution(3,7);
    uniform_real_distribution<double>rand_distribution2(1,10);


But now I get Thread 1: EXC_BAD_ACCESS at line 38 of the main script. Which is when I call the function. Any idea what's wrong?
You are accessing vector elements out of bounds.
Got it. Thanks!
Topic archived. No new replies allowed.