Same random numbers from method call

I'm having trouble with a part of a method I am trying to write that first generates a random number to determine the number of line items that will be part of an order, and then a loop to to randomize an inventory number that will be filled into each of the arrays elements. There will also be a method/statement to determine that their are no duplicate inventory numbers. When I am filling the array with an inventory number it just keeps using the same number as the number of line items. The intention is each time the loop iterates that the method call to the random number function will be different? I feel like this works fine when use in the main method but doesn't appearing to be working here. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
void populateOrder()
{
    int lineItems = getRandom(1, 10);
    int i;
    vector<int> pickList(lineItems);

    for (i = 0; i < lineItems; i++)
    {
        int inventoryNumber = 0;
        inventoryNumber = getRandom(1, 10);

        pickList[i] = inventoryNumber;

        /*
        for (int dup = 0; dup < lineItems; dup++)
        {
            while (inventoryNumber == pickList[dup])
            {
                int inventoryNumber = getRandom(1, 10);
            }


        }
        */
    }

    for (i = 0; i < lineItems; i++)
    {
        cout << pickList[i] << endl;
    }

}

int getRandom(int minimum, int maximum)
{

    cout << "getting random\n";
    srand((unsigned)time(0));
    int randomNumber = (rand() % maximum) + minimum;

    return randomNumber;
    
}



Example out run 1

getting random
getting random
getting random
getting random
3
3
3



Example out run 2

getting random
getting random
getting random
getting random
getting random
getting random
getting random
6
6
6
6
6
6

Last edited on
Figured it out. I didn't realize how important it was in C++ to include the seed in the main to ensure a random number. This was a habit unfortunately that I carried over from Java where having a dedicated method for random numbers would work in this capacity.
Technically, rand() and srand() are C functions. C++ has different approach to random: http://www.cplusplus.com/reference/random/
^^ but the seeding is similar, you only want to seed once unless you have a reason to repeat from the start with the same seed, which can be a nice tool for some programs. I highly advise wrapping up the c++ random tool into a wrapper class.
Last edited on
Thanks for the tip. Okay so the next thing I ma having trouble with is my nested loop that steps through each of arrays elements to compare to prior elements and makes sure there are no identical elements, otherwise it creates a random number again. I have tried using a while loop for this but it just keeps repeating.


void populateOrder()
{
    int lineItems = getRandom(1, 10);
    int i;
    vector<int> pickList(lineItems);

    for (i = 0; i < lineItems; i++)
    {
        int inventoryNumber = 0;
        inventoryNumber = getRandom(1, 10);

        pickList[i] = inventoryNumber;
        for (i = 0; i < lineItems; i++)
        {
            int inventoryNumber = 0;
            inventoryNumber = getRandom(1, 10);

            pickList[i] = inventoryNumber;

            
            for (int dup = 0; dup < lineItems; dup++)
            {
                if (pickList[i] == pickList[dup])
                {
                    pickList[i] = getRandom(1, 10);
                }
            }
            
        }
        
    }


I think there is some in the logic I am not quite catching onto. But I'd like to self array comparison to be contained within each loop that generates a random number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#include <ctime>

int getRandom(int minimum, int maximum)
{
    return (rand() % maximum) + minimum;
}

void populateOrder(std::vector<int> &aPickList)
{
    int size = aPickList.size();
    int number{0};
    
    std::vector<int> duplicate_checker(10);
    for (int i = 0; i < size; i++)
    {
        do
        {
            number = getRandom(1, 10);
        }
        while (duplicate_checker[number - 1] == 1);
        
        duplicate_checker[number - 1] = 1;
        aPickList[i] = number;
    }
}


int main()
{
    srand (time(NULL)); // <--
    
    int lineItems = getRandom(1, 10);
    std::vector<int> pickList(lineItems);
    populateOrder(pickList);
    
    // DISPLAY LIST
    std::cout << lineItems << ": ";
    for(auto i:pickList)
    {
        std::cout << i << ' ';
    }
    
    return 0;
}
"Shuffle and deal" http://www.cplusplus.com/reference/algorithm/shuffle/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// shuffle algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::shuffle
#include <vector>       // std::vector
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock
#include <numeric>      // std::iota

int main () {
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator( seed );

  std::vector<int> foo( 10 );
  std::iota( foo.begin(), foo.end(), 1 ); // create numbers [1..10]
  shuffle( foo.begin(), foo.end(), generator ); // random order

  std::uniform_int_distribution<int> distribution(1, std::min(10UL, foo.size()) );
  int lineItems = distribution( generator );
  foo.resize( lineItems ); // keep only lineItems numbers

  std::cout << "shuffled elements:";
  for ( int& x : foo ) std::cout << ' ' << x;
  std::cout << '\n';
}

checking to see if you have like 5 items out of 100 (that is 0-100 possible, pick 5 of them) in a loop like that works pretty well. trying to randomly roll 95 values in 0-100, however, will loop a long time as you keep getting repeats until the whim of the random generator produce the last couple of values, which only have around a 5% chance. Shuffle and deal has a 100% chance of working every time, so it can't get stuck trying to beat the odds. And it avoids having to track and check the values by ensuring that you can't get a repeat to begin with. All around, is the way to do it.

Last edited on
Topic archived. No new replies allowed.