convert from python to c++

scotty (14)
.
Last edited on
JLBorges (1752)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <random>
#include <ctime>
#include <iostream>

int main()
{
    std::vector<int> m(11) ;
    std::mt19937 rng( std::time(nullptr) ) ;
    std::uniform_int_distribution<> distr( 1, 6 ) ;

    for( int i = 0 ; i < 1000 ; ++i ) ++m[ distr(rng) + distr(rng) - 2 ] ;

    std::cout << "[ " ;
    for( int i : m ) std::cout << i << ' ' ;
    std::cout << "]\n" ;
}

Last edited on
Smac89 (195)
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
#include <stdlib.h>
#include <iostream>
#include <vector>

int main ()
{
    srand(unsigned(time(NULL)));
    
    std::vector<int> Dij(11, 0);
    
    int A;
    int B;
    
    for (int r = 0; r < 1000; r++)
    {
        A = rand() % 6 + 1;
        B = rand() % 6 + 1;
        
        Dij.at(A+B - 2) += 1;
    }
    
    std::cout << "[ ";
    for (int w = 0; w < (int)Dij.size(); w++)
        std::cout << Dij.at(w) << " ";
    
    std::cout << "]" <<std::endl;
    return(0);
}
L B (3805)
What was wrong with JL's?

EDIT: The OPer deleted his post, could someone repost it?
Last edited on
Smac89 (195)
Op deleted thread ^

Nothing wrong with his, just wanted to post mine

Here:

mport numpy as nu
import pylab as py
m=nu.zeros(11,int)
for i in range(1000):
A=nu.random.randint(6)+1
B=nu.random.randint(6)+1
n=A+B-2
m[n]=m[n]+1
print m
so that produces an array in the form
[ 32 44 81 106 126 172 138 119 93 58 31]
where the 1st position corresponds to the number of 2s the second the number of 3s the third the number of 4s and so on....
m starts as [0,0,0...]
then m[n] corresponds to the position in the array with m[0] being the 1st position
third line could be m=[0,0,0,0,0,0,0,0,0,0,0]

Needed it to be converted to c++
Last edited on
scotty (14)
When I put JL's into Microsoft Visual studio it says:

Error 1 error C2039: 'mt19937' : is not a member of 'std'
Error 2 error C2065: 'mt19937' : undeclared identifier


any help?

and Smac89 - when I put yours in it only shows the vector..
L B (3805)
@Scotty you need a compiler that supports C++11

In MSVC I think you can get some things through the tr1:: namespace
http://msdn.microsoft.com/en-us/library/bb982198(v=vs.90).aspx
scotty (14)
I've got to write it in Microsoft Visual Studio 2010 in c++ for my assignment unfortunately.
Registered users can post here. Sign in or register to post.