-1

I'm trying to print random numbers using srand. This is my code.

I want each subsequent number larger than the previous number in the list.

This is my code.

#include <iostream>
#include <ctime>
#include <cmath>
#include <random>
#include <vector>
#include <algorithm>
#include <climits> // for INT_MAX;


int main(){
srand((int)time(0));

std::vector<int> myVec;


int number = 5;
for(size_t index = 0; index <100; index++){
  int  result = (rand() % number) +1;
  if((result % 5) == 0 or (result % 5 == 3 && index == 50)){
    number += 2;
    myVec.push_back(number);
}
}

for(int vec : myVec){
    std::cout << vec << "  ";
}

return 0;
}

The output I'm looking for is

0 2 4 6 9 12 so on

Can someone please guide me on how can I achieve it?

Justin
  • 177
  • 8
  • 1
    Doe your code compile? `or` is not a valid C++ operator. What specific error or incorrect behaviour are you encountering? Also, why are you sorting an empty vector? – kaylum Apr 25 '22 at 23:35
  • 1
    Presumably `myVec.push_back(result);` should be `myVec.push_back(number);` – kaylum Apr 25 '22 at 23:36
  • Yes, the code compiles, I thought I wanted to sort it as well. But It doesn't work really. – Justin Apr 25 '22 at 23:36
  • That is acully currect – Justin Apr 25 '22 at 23:37
  • but it is not random? – Justin Apr 25 '22 at 23:37
  • This is the compilation shows. – Justin Apr 25 '22 at 23:40
  • https://onlinegdb.com/BPv3hYWef – Justin Apr 25 '22 at 23:40
  • 1
    You've presented the goal of your program, the desired output of your program, and the code itself. What you are lacking is the actual output, or any indication that the code you have fails to meet your goals. – JaMiT Apr 25 '22 at 23:42
  • 1
    `number += 2;` should be `number = result + 2` or just `myVec.push_back(result+2);`. As it is, you are just continuously increasing `number`. Now would be a good time to learn to debug your own code as these issues should be easily found even with any basic debugging. Run your program in a debugger. – kaylum Apr 25 '22 at 23:43
  • Yes I know, @JaMiT That is correct, but one should be able to ask others if he/she doesn't know the answer or is stuck with the problem. I thought to ask here if anyone else could help me. – Justin Apr 25 '22 at 23:46
  • 1
    @Justin *"one should be able to ask others if he/she doesn't know the answer or is stuck with the problem"* -- well, yes, but what does that have to do with anything? You're saying that you don't know what happens if you try to compile and run your own code? That's what I requested -- your actual results. Take your code, compile it, run it, and observe what happens. Tell us -- in your question -- what you observed that led you to believe that your current code does not accomplish your goal. – JaMiT Apr 25 '22 at 23:51
  • Thanks, @JaMiT I see your point! I shared the link you can check it if you like. The result I'm getting is not what I'm looking for I want it to be random. That is the problem if they were not random numbers I could solve it 20 minutes ago. – Justin Apr 25 '22 at 23:54
  • Thanks anyway. everyone. I think I just delete the question. All the best! – Justin Apr 25 '22 at 23:56
  • 1
    Are you just trying to generate random numbers where each random number is 0-4 greater than the subsequent number? If so, could you clarify what your if statement is attempting to accomplish? Why `result % 5 == 3 && index == 50`? – jts Apr 25 '22 at 23:57
  • Yes, that is correct! That is what I want. But it won't work. )= – Justin Apr 25 '22 at 23:58
  • 1
    Don't delete your question if you want. Posting a possible solution in a minute. – jts Apr 25 '22 at 23:58
  • 1
    @kaylum actually 'or ' is a valid alternative to '||' in c++. I learned that a few weeks ago when I made a similar comment – pm100 Apr 26 '22 at 00:20

1 Answers1

2

You should be able to write this a lot simpler. Also, the reason your code seems to be not getting any random values is because your pushing back your bound with myVec.push_back(number). You should instead be doing myVec.push_back(result).

As for your actual question, it doesn't seem like your code is written as to what you're trying to achieve. If you want to get values 0-4 greater than the subsequent value, it would be better to write it something like this:

#include <iostream>
#include <ctime>
#include <random>
#include <vector>

int main(){
    srand(time(0));

    std::vector<int> nums;

    int lower = 0;
    for(int i=0; i<100; i++) {
        // generate random number from lower bound to upper bound
        // starting lower bound is 0
        nums.push_back((rand() % 5) + lower);
        lower = nums[nums.size()-1];
    }

    for(int num : nums){
        std::cout << num << "  ";
    }
    std::cout << "\n";
    return 0;
}

So, generally, if a random function is returning any value x where 0 <= x < 1 (which I believe C/C++ rand() does), then to get a value within a given range you want to have it in this format:

(rand() % (upper_bound - lower_bound + 1)) + lower_bound

However, you said that you want all values to be 0-4 greater than the lower bound. This means we essentially want to start from our lower bound, and add 0-4 to it. So really, we just need to generate a random number from 0-4. The code above does rand() % 5 because rand() % N will return any number x where 0 <= x < N. So, we want to do rand() % 5 which will get any number from 0 to 4.

Also, this is just semantics, but in my personal experience it's always good to try and maintain some level of inference with your variable naming. In your for-each loop you wrote for(int vec : myVec), but to the glancing eye this may appear that myVec is actually a vector of vectors, and every element of myVec is a vector itself. Although the type int is clearly declared, it's still easy to get confused as to why the developer named it "vec" if its type is not a vector. Once again, some of this comes down to preference and if this is for a personal project, assignment, or actual production code. But I'd start making a good habit of making sure things are named intentionally and purposefully.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
jts
  • 161
  • 1
  • 9
  • Thank you Jts! I wonder if there is a way to avoid duplicates? – Justin Apr 26 '22 at 00:24
  • 1
    Sure! If you want to avoid duplicates, just make sure that the range being generated is from 1-4 instead of 0-4. If you do this, you'll need to generate a random number from 0-3, and then add 1 to it, which will generate a random number from 1-4. That looks like this: `(rand() % 4) + 1` edit: forgot you wanted it to be from a lower bound, would technically look like this: `(rand() % 4) + lower + 1` – jts Apr 26 '22 at 00:25
  • Thank you again! Is there any book you recommend to new CS students? Any useful websites or anything you wish you knew before starting CS? I love to know your experience if you could please share some word please! – Justin Apr 26 '22 at 00:31
  • 1
    I'm not much of a book reader, but as for learning programming/CS topics YouTube and social media are always great options. If you really wanna read, or look specifically into C/C++, this stackoverflow post has some excellent resources! https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jts Apr 26 '22 at 00:34