Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:44:01+00:00 2026-06-10T16:44:01+00:00

I am teaching myself Rcpp and notice that Rcpp sugar does not have sample

  • 0

I am teaching myself Rcpp and notice that Rcpp sugar does not have sample function.
So I decided to call sample function in base library from C++.
I have two questions:

1.
Regarding the type of the arguments prob, am I supposed to use NumericVector?
Is it legall to use vector type?

2.
Regarding the type of the output, am I supposed to use IntegerVector?
Is it legall to use NumericVector type?

It seems all these types are fine (See the code below) but I would like to know which type is better to use.

<!-- language-all: lang-html -->
library(inline)
library(Rcpp)

src1 <- '
   RNGScope scope;

  NumericVector thenum(1),myprob(3);

  myprob[0]=0.1;
  myprob[1]=0.5;
  myprob[2]=0.4;

  Environment base("package:base");
  Function sample = base["sample"];

  thenum = sample(3,Named("size",1),Named("prob",myprob));

  return wrap(thenum);
'


src2 <- '
  RNGScope scope;

  IntegerVector theint(1);
  vector<double> myprob(3);
    myprob[0]=0.1;
  myprob[1]=0.5;
  myprob[2]=0.4;
  Environment base("package:base");
  Function sample = base["sample"];

  theint = sample(3,Named("size",1),Named("prob",myprob));

  return wrap(theint);
'


fun1 <- cxxfunction(signature(),body=src1,plugin="Rcpp")
fun2 <- cxxfunction(signature(),body=src2,include='using namespace std;',plugin="Rcpp")

fun1() ## work!
fun2() ## oh this works too! 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:44:03+00:00Added an answer on June 10, 2026 at 4:44 pm

    Because you are calling sample() from R, both integer and numeric works as they do in R itself:

    R> set.seed(42); sample(seq(1L, 5L), 5, replace=TRUE)
    [1] 5 5 2 5 4
    R> set.seed(42); sample(seq(1.0, 5.0), 5, replace=TRUE)
    [1] 5 5 2 5 4
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been teaching myself C++ and someone told me that C++ does not have
Teaching myself some c# and my problem is that I have a class which
I have been teaching myself JavaScript over the last Month now, not super consistently
I'm teaching myself Python and my most recent lesson was that Python is not
I have been teaching myself JS and Jquery primarily for GM scripts (im not
I have recently started teaching myself C# and Asp.net. I am trying to build
I'm teaching myself programming and today's challenge is to write a program that can
I am teaching myself to use JavaCC in a hobby project, and have a
Teaching myself C and finding that when I do an equation for a temp
I'm still teaching myself how to bind and use observable collection. One problem that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.