Filling an array with random numbers using srand(int(time(0)))

I have searched the internet and have not been able to find a definitive answer.
I have a general idea but when trying to return the values to the array it outputs garbage. I assume I have tried everything but the correct thing.
I was wondering what the general structure of a srand(int(time(0))) in an array based list would be. If more information is needed I can do. I also know that random numbers have been posted but I can't seem to find them for this particular problem.

Thanks
The srand() function only initializes the RNG. You only need to do it once - usually somewhere at the beginning of main().

After that, you use rand() to get numbers.

Hope this helps.
I understand the part about putting srand in the main function to initialize the seed. Is there anyway I could post my code and have you look at it and maybe tell me why I'm getting garbage. Or email it to you.
Last edited on
Why not post the code here? If it's too long try forming a shorter version that reproduces the problem.
I know that this isn't right but this is what I left it at.

Thanks again!




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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MLS = 50;
const int SENTINEL = -1;
typedef int element;

class AList {
        private:
                element items[MLS];
                int size;
                int Read_int();
                void Swap(int pos1, int pos2);
                element Read_element();
                void exit_method();
        public:
                void Menu();
                void Read();
                void Ran_Gen();
                void BubbleSort();
                void SelectionSort();
                void InsertionSort();
                void Print();
                void LinearSearch(element target,bool&found,int&position);
                void BinarySearch(element target,bool&found,int&position);
};

int main() {
        srand(int(time(0)));
        cout<< "\n\nSort and Search Demo Program, version 1.0\n"
            << "(c) 2011,\n\n";

        AList B;
                B.Menu();
}

void AList::Menu() {
        element menuchoice;

       element target_linear;
        bool found_linear;
        int position_linear;

        element target_binary;
        bool found_binary;
        int position_binary;

        const int keyboard=1,
                  random=2,
                  bubble=3,
                  insertion=4,
                  selection=5,
                  linear=6,
                  binary=7,
                  exit=8;
        do {
        cout<< "Current List: ";
        if (menuchoice>0)
                Print();
        else
                cout<< "(empty)\t";
        if ((menuchoice>2)&&(menuchoice<6))
                cout<<"(list is known to be ordered)\n\n";
        else
                cout<<"(list is not know to be ordered)\n\n";
        cout<< "Actions:\n";
        cout<< "  1.  Reset the current list from the keyboard\n";
        cout<< "  2.  Reset the current list using randomly generated "
            << "elements\n";
        cout<< "  3.  Perform Bubble Sort on the current list\n";
        cout<< "  4.  Perform Insertion Sort on the current list\n";
        cout<< "  5.  Perform Selection Sort on the current list\n";
        cout<< "  6.  Perform Linear Search on the current list\n";
        cout<< "  7.  Perform Binary Search on the current list\n";
        cout<< "  8.  Quit the program\n\n";

        cout<< "Choose an action:  ";
        menuchoice=Read_int();

   cout<< endl;
        switch (menuchoice) {
                case 1:
                Read();
                break;
                case 2:
                Ran_Gen();
                break;
                case 3:
                BubbleSort();
                break;
                case 4:
                InsertionSort();
                break;
                case 5:
                SelectionSort();
                break;
                case 6:{
                LinearSearch(target_linear,found_linear,position_linear);
                }
                break;
                case 7: {
                BinarySearch(target_binary,found_binary,position_binary);
                cout<< "The target was "<<found_binary<< " on the"
                    << " current list in position "<<position_binary;
                cout<< endl<<endl;
                }
                break;
                case 8:
                exit_method();
                break;
                default: cout<< "Please enter a valid menu choice: ";
                menuchoice=Read_int();
                ;
                }
        }

        while (menuchoice !=8);

}

void AList::Print() {
        for (int i=0; i<size; i++)
                cout<< items[i]<<" ";
}

void AList::Ran_Gen() {
        int usersize;
        int max;
        int min;
        int x;
        
  cout<< "Enter low: ";
        cin>> min;
        cout<< "Enter high: ";
        cin>> max;
        cout<< "Enter size: ";
        cin>> usersize;



for (int i = 0; i <usersize; i++)
       x = rand()+(max-min+1)+min;

        size = x;


Last edited on
Could you post the code within [code][/code] tags? Also, in this code you never assign any values to the elements in items.
I think I fixed that when I edited the code if not please let me know. I am also aware that I never assigned any values to the elements in items at this point I was just trying to get the random numbers to display.
Last edited on
Nope, the code is still not within tags. You can physically place the tags around the code in your post or you could select the code and press the <> button on the right side.

It looks like your comparison here is backwards:

for (int i=0; usersize<i; i++)

It should be i < usersize.

Also, it looks like x is never declared though that should give you a compilation error. Another thing is it will only print the last x you've generated because the for loop only does the one line after it (unless you use {} to scope the loop).
Oh I see about the code thing, thanks! Yeah I think it best that I change the code back to the way I had it give me one sec. This is my first time posting.... sorry about the crappie posting I'll fix the code so it was semi the way it was supposed to be.
Alright I think I changed the above code to what I think it should be sorry about the confusion!
Now, usersize, max, and min are not given values at all.

Also, this:

rand()+(max-min+1)-min;

will not give you a number between min and max. Instead of +(max-min+1) you want to do % (max-min+1). The % operator will bring the number returned from rand() down to the range [0, max-min+1-1] inclusive. Adding on the other hand like you had would just add to whatever (large) number rand() returned. You also don't want to subtract min at the end, but rather you should add it. This will bring your number to between [min, max] inclusive. If you subtract min at the end, then your number would be in the range [-min, max-2*min].

Your for loop is still executing only one statement. size would only be given the last number generated.
alright I changed those and if I understand you correctly I should get at least one value but I am still getting garbage.
srand() takes an unsigned int. Try casting to an unsigned int instead of an int. Are you getting garbage when you print x? Because the code is incomplete and I don't see any printing of the random numbers.

Also, you're not giving menuchoice a value before it's first used.
Last edited on
So I need a separate method for printing random numbers, and I'm not exactly sure what you mean by not giving menuchoice a value before it's used. I can't just put them into the array items.

For me it was easier to fill the array with numbers, and then shuffle their order. It goes like this.

//To fill the array with numbers.

int numbers[10];
int x=0;
while (x<10) { number[x]=x+1; x++;}

int c=0;
int r;
int p;
srand ( (unsigned)time( NULL ) );

while (c<10) { // do this 10 times
r=(rand() % 10+1) // d is a random number between 1 and 10
p=number[c]; //keeps track of what number[c] was;
number[c]=number[r]; //switch number[c] with number[random number between 1-10]
number[r]=p; //switch number[d] with what c was before it was switched
c++;}

if you want to print the whole array,

for (x=0;x<10;x++) {cout << number[x] << " ";}
Last edited on
The code above is good for if you don't want one number in the array more than once.

If you just want an array of random numbers, then you can just use:

int x=0;
int n[10];

srand ( (unsigned)time( NULL ) );

while (x<10) {n[x]=(rand() % 10+1);}
I'm not exactly sure what you mean by not giving menuchoice a value before it's used


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void AList::Menu() {
        element menuchoice; //you've declared menuchoice here but it does not have a value (it is uninitialized)

       element target_linear;
        bool found_linear;
        int position_linear;

        element target_binary;
        bool found_binary;
        int position_binary;

        const int keyboard=1,
                  random=2,
                  bubble=3,
                  insertion=4,
                  selection=5,
                  linear=6,
                  binary=7,
                  exit=8;
        do {
        cout<< "Current List: ";
        if (menuchoice>0) //then you use menuchoice here but you have not assigned it a value yet
                Print();


Also, strange coincidence: http://www.cplusplus.com/forum/general/53553/#msg289574
Last edited on
Topic archived. No new replies allowed.