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 6784609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:00:59+00:00 2026-05-26T17:00:59+00:00

I would like to use C++11’s variadic templates to achieve a generalized random picker

  • 0

I would like to use C++11’s variadic templates to achieve a generalized “random picker” function.

Something like this…

template <typename T>
T randomPicker(T one, T two, T three)
{
    int pick = 3 * (rand() / double(RAND_MAX));
    switch (pick)
    {
        case 0:
            return one;
        case 1:
            return two;
        default:
            return three;
    }
}

… except generalized to accept any number of parameters (each of the same type, as above — although accepting any type as a parameter and converting the chosen one to some specific type T upon return would be acceptable also).

I understand the idea of using template recursion to achieve things like the typesafe printf, etc. Can variadic templates also be used to create the sort of function described above? Any tips appreciated!

  • 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-05-26T17:00:59+00:00Added an answer on May 26, 2026 at 5:00 pm

    Something like this, although I can’t test it:

    template <typename First, typename... Others>
    First randompicker(First first, Others ...args) {
        const size_t len = sizeof...(args) + 1;
        if (rand() / double(RAND_MAX) < 1.0 / len) {
            return first;
        }
        return randompicker(args...);
    }
    
    template <typename Only>
    Only randompicker(Only only) {
        return only;
    }
    

    I’m not sure whether the overload there is right — presumably a parameter pack can be empty, I don’t know whether I can still overload for one argument without ambiguity.

    Admittedly this uses more random numbers than your example with 3 args, and may be more sensitive to bias from rounding errors. So, you could pick a random number from 0 to len-1 at the start, and then call a recursive function that selects the nth argument out of the parameter pack:

    template <typename First, typename... Others>
    First select(size_t idx, First first, Others ...args) {
        if (idx == 0) return first;
        return select(idx-1, args...);
    }
    
    template <typename Only>
    Only select(size_t, Only only) {
        return only;
    }
    
    template <typename First, typename... Others>
    First randompicker(First first, Others ...args) {
        static std::default_random_engine re;
    
        const size_t len = sizeof...(args) + 1;
        std::uniform_int_distribution<size_t> range{0, len - 1};
    
        const size_t idx = range(re);
        return select(idx, first, args...);
    }
    

    In all cases, I have n if/else statements instead of an n-way switch. You might be lucky with the optimizer, or you might be able to “unroll the loop” a bit by having First, Second … A few parameter args before the variable args.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a single haml/mustache template, like this: .foo %h2 {{title}}
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use jQuery's load function. I can write: $('#somediv').load('somefile.html #aSpecificDivinThatPage'); Now
I would like use .appendTo() on different elements determined by a function. For example,
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to use a language that I am familiar with - Java,
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are

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.