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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:49:18+00:00 2026-05-25T23:49:18+00:00

I am having trouble using srand. i am trying to generate a random number

  • 0

I am having trouble using srand.

i am trying to generate a random number in the interval 100 to 200.

The number will keep being generated and placed in an array. Once the method is called again the same sequence of random numbers needs to be generated again.

Because of this I need a seed, no matter what I try I cannot seem to get it to work.

I am not looking for anyone to write some code rather just show me the correct formatting for generating such numbers.

UPDATE

I have a train object, which contains a linkedlist(each position in the linedlist is a carriage).

The number of carriages in each train needs to be random in the interval 100, 200.

The amount of coal in each carriage needs to be random in the interval 1000, 2000.

I am trying to implement a simulator class that will create a train with a random amount of carriages which contain a random amount of data.

Hope that makes a bit more sense.

Struggling on how to implement it.

  • 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-25T23:49:18+00:00Added an answer on May 25, 2026 at 11:49 pm

    If you just want to repeat an arbitrary sequence, you can set the seed with srand() by giving it the same argument.

    For example:

    pax$ cat qq.c
    #include <iostream>
    #include <cstdlib>
    
    int main (void) {
        srand (42);
        for (int i = 0; i < 5; i++) {
            int x = 100 + (rand() % 101);
            std::cout << x << std::endl;
        }
        std::cout << "=====" << std::endl;
        srand (42);
        for (int i = 0; i < 5; i++) {
            int x = 100 + (rand() % 101);
            std::cout << x << std::endl;
        }
        return 0;
    }
    
    pax$ g++ -o qq qq.cpp ; ./qq
    163
    166
    148
    137
    149
    =====
    163
    166
    148
    137
    149
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am having trouble using multiquery in facebook .net. I am trying to
I am having trouble using script/generate . I am following the tree based navigation
LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm
Im having trouble using a .NET COM in vb6, It compiles ok and I
I am using mssql and am having trouble using a subquery. The real query
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a
I'm having trouble using os.utime to correctly set the modification time on the mac
I am having trouble using EnumSet on the client side. I get this runtime

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.