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

The Archive Base Latest Questions

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

Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving

  • 0

Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving me the same values but with a small change in the decimal field. ex : 27.xxx.

Heres my code, any help would be appreciated.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand( time(NULL) );
    double r = (68.556*rand()/RAND_MAX - 32.768);
    cout << r << endl;
    return 0;
}
  • 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-18T02:23:15+00:00Added an answer on June 18, 2026 at 2:23 am

    I should mention if you’re using a C++11 compiler, you can use something like this, which is actually easier to read and harder to mess up:

    #include <random>
    #include <iostream>
    #include <ctime>
    
    
    int main()
    {
        //Type of random number distribution
        std::uniform_real_distribution<double> dist(-32.768, 32.768);  //(min, max)
    
        //Mersenne Twister: Good quality random number generator
        std::mt19937 rng; 
        //Initialize with non-deterministic seeds
        rng.seed(std::random_device{}()); 
    
        // generate 10 random numbers.
        for (int i=0; i<10; i++)
        {
          std::cout << dist(rng) << std::endl;
        }
        return 0;
    }
    

    As bames53 pointed out, the above code can be made even shorter if you make full use of c++11:

    #include <random>
    #include <iostream>
    #include <ctime>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        std::mt19937 rng; 
        std::uniform_real_distribution<double> dist(-32.768, 32.768);  //(min, max)
        rng.seed(std::random_device{}()); //non-deterministic seed
        std::generate_n( 
             std::ostream_iterator<double>(std::cout, "\n"),
             10, 
             [&]{ return dist(rng);} ); 
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently having trouble with German umlaut values in a XML document I
I'm currently having trouble with XMLParse() , as the title says, it keeps throwing
I am currently having trouble reading multiple values from a server. This is a
I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming
I am currently having trouble with a live wallpaper that does some very intensive
I'm currently having trouble creating an image from a binary string of data in
I am currently having trouble finding the index/location of a button in a table
I'm currently having trouble with a destructor of a class which contains a vector
The part of the application that I am currently having trouble getting to work
I am currently having unnecessary trouble trying to test out RubyMine. My biggest problem

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.