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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:49:46+00:00 2026-05-24T01:49:46+00:00

I have a simple integer vector, with only 4 values. I want to loop

  • 0

I have a simple integer vector, with only 4 values. I want to loop through the vector, assigning either 0 or 1 to each value. I want it to be random to the point that its different every time.

I thought the following would suffice:

  for (int i = 0; i < (int)numberVect.size(); i++)
  {
      numberVect[i] = rand() % 2;
  }

However, what I found was that every time I would close and re-open the program, the exact same values would be assigned.

So the first time through it would be 1,0,0,0, then if I ran the loop again without closing the program it would be 0,1,0,1. which seems perfect.

HOWEVER, after closing the program and restarting it up again, I would find the first sequence would again be 1,0,0,0 and the second would again be 0,1,0,1.


Also I have a second question in relation to this problem:

Can you suggest a way to make sure that AT LEAST one of the values in the vector is a 1? while still allowing the random generation to work seamlessly?

Thanks a lot and I hope you guys can help 😀

  • 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-24T01:49:47+00:00Added an answer on May 24, 2026 at 1:49 am

    You need to seed the random generator, before using it. For that you need srand()

    I usually do that with passing current time to srand() as:

    #include <time.h>
    
    srand(time(NULL)); //seeding!
    
    //now use
    int whatever = rand();
    

    Now everytime, you run this code you’ll get different random sequence,even if there is one second of difference between two consecutive runs.

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

Sidebar

Related Questions

I have a very simple iPhone app that requires a random integer from 1-100.
We have a column that is a simple integer. We want to add to
I have a simple one dimmensional array of integer values that represent a physical
I have a simple for loop with the following code For i As Integer
I have a map which contains integer values. i want to re-arange this map
If I have a simple piece of data to store (an integer or string
I have this simple regex, [\d]{1,5} that matches any integer between 0 and 99999.
Let's say I have two simple models project t.string :title vote t.references :project t.integer
During program optimization, trying to optimize a loop that iterates through a vector, I
I want to create a simple integer range checker and converter using c++ templates.

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.