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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:56:18+00:00 2026-06-14T21:56:18+00:00

What is the best seeded random number generator on ObjC? I have to generate

  • 0

What is the best seeded random number generator on ObjC?

I have to generate the same random number sequences on 2 different iPhones, therefore it has to be seeded. This is why I can’t use arc4rand().

NOTE: by best I mean fastest/most unpredictable relation.

  • 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-14T21:56:20+00:00Added an answer on June 14, 2026 at 9:56 pm

    The Mersenne Twister implementation that comes with the C++ standard library is very good. As it’s C++, you’ll need to create a wrapper so that you can call it from C and ObjC code, or alternatively rename the file that uses it to have a .mm (Objective-C++) extension.

    I’m thinking of something like this, in the header:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    struct rng_state;
    struct rng_state* create_rng(unsigned seed);
    void destroy_rng(struct rng_state* rng);
    unsigned long long rng_random_unsigned(struct rng_state* rng, unsigned long long max);
    
    #ifdef __cplusplus
    }
    #endif
    

    And then, in a .cpp file that includes the above:

    #include <random>
    struct rng_state
    {
      std::mt19937* rng;
    };
    struct rng_state* create_rng(unsigned seed)
    {
      std::mt19937* engine = new std::mt19937(seed);
      rng_state* state = new rng_state;
      state->rng = engine;
    }
    void destroy_rng(struct rng_state* rng)
    {
      delete rng->rng;
      delete rng;
    }
    unsigned long long rng_random_unsigned(struct rng_state* rng, unsigned long long max)
    {
      std::uniform_int_distribution<unsigned long long> distribution(0, max);
      return distribution(*rng->rng);
    }
    

    I haven’t tested the above, but it should be pretty close. You can then include the header in your C/ObjC files as usual, create a rng with a seed, get a bunch of random numbers, and destroy the rng when you’re done. You can also add more generator functions if needed – the library comes with different random distributions you can use.

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

Sidebar

Related Questions

Best to describe this in code... I have this public class A<T> { public
Hi I have been trying to work out the best way to do this
Best explained with code I think, this is just a simple example: public class
Best to use an example to describe the problem. Lets say I have a
Best practices or tools for installing a SQL Server database I have a SQL
I have had this problem for a while, still trying to work on a
I have a widget that currently takes a random string from an array and
Quick question on best practice. I have an AJAX application where there are 4
I am wondering what's the best practice to parse XML like this: <root> <MailNotification
I am trying to create variable number of sparse matrices. At first the best

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.