Random numbers without repition

Mar 29, 2011 at 11:52pm
I'm writing a program that requires an array of scrambled numbers that don't repeat when given a maximum value. I've been searching on the internet for hours and every pice of code I find returns some hexidecimal value. Help?
Mar 30, 2011 at 12:12am
You need to explain a bit better but this should help: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Mar 30, 2011 at 12:14am
I've been searching on the internet for hours and every pice of code I find returns some hexidecimal value.

any examples of these?
Mar 30, 2011 at 1:33am
Apr 6, 2011 at 6:23pm
sorry for taking so long to reply.

heres some code I wrote after thinking about it more, but it still returns hexidecimal values. (im using xcode fyi) maybe its a problem with my compiler? all of the code makes sence...
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
//
//  main.cpp
//  ScrambleNums
//
//  Created by Jake on 4/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])
{
    int nStart[5], nFinish[5];
    bool bCheck[5];
    
    for (int i = 0; bCheck[i]; i++) {
        bCheck[i] = false;
    }
    
    for (int i = 0; i<5; i++) {
        nStart[i] = i+1;
    }
    cout << nStart << endl;
    
    for (int i = 0; nStart[i]; i++) {
        int random = rand() % 6;
        
        if (bCheck[random] == false) {
            bCheck[random] = true;
            nFinish[i] = nStart[random];
        }
        
        if (bCheck[random] == true) {
            for (;;)
            {
                random = rand() % 6;
                
                if (bCheck[random] == false) {
                    bCheck[random] = true;
                    nFinish[i] = nStart[random];
                    break;
                }
                
            }
        }
    }
    
    cout << nFinish << endl;
    return 0;
}

Apr 6, 2011 at 6:49pm
but it still returns hexidecimal values.

What do you expect to see when you print a pointer?
1
2
3
    int nStart[5], nFinish[5];
...
    cout << nFinish << endl;

Apr 6, 2011 at 9:02pm
It's not a pointer...
Apr 6, 2011 at 9:08pm
Well it's an array, and thus 'nFinish' is a pointer to the first element of the array, equivalent to '&nFinish[0]'.

You generate more than one random integer it seems, you you will need to loop through to print them all to standard output.
Apr 6, 2011 at 9:13pm
I tried that, It doesn't seem to work. When it runs, it doesn't display anything.
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
#include <iostream>

using namespace std;

int main (int argc, const char * argv[])
{
    int nStart[5], nFinish[5];
    bool bCheck[5];
    
    
    for (int i = 0; bCheck[i]; i++) {
        bCheck[i] = false;
    }
    
    for (int i = 0; nFinish[i]; i++) {
        nStart[i] = i+1;
    }
    
    for (int i = 0; nStart[i]; i++) {
        int random = rand() % 7;
        
        if (bCheck[random] == false) {
            bCheck[random] = true;
            nFinish[i] = nStart[random];
        }
        
        if (bCheck[random] == true) {
            for (;;)
            {
                random = rand() % 7;
                
                if (bCheck[random] == false) {
                    bCheck[random] = true;
                    nFinish[i] = nStart[random];
                    break;
                }
                
            }
        }
    }
    
    for (int i = 0; nFinish[i]; i++) {
        cout << nFinish[i];
    }
 
    return 0;
}
Apr 6, 2011 at 9:23pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for (int i = 0; bCheck[i]; i++) {
        bCheck[i] = false;
    }
    
    for (int i = 0; nFinish[i]; i++) {
        nStart[i] = i+1;
    }
    
    for (int i = 0; nStart[i]; i++) {
       ...
    }
    
    for (int i = 0; nFinish[i]; i++) {
        cout << nFinish[i];
    }


These "for" loops are all wrong. http://cplusplus.com/doc/tutorial/control/

The second "piece" (don't know what else to call it) is the condition. If the condition evaluates to true, then the for loop runs again. If it's false, then the for loop terminates and the program's control flow continues.

You want to use the amount of elements in the array (which is 5 in each).

1
2
3
4
for(int i = 0; i < 5; ++i) { // Loops through 5 times (i goes from 0 through 4)
    bCheck[i] = false;
}
...


EDIT: One more thing:

It's advisable to use "srand" once at the top of main to get a different set of numbers every time the program is run. http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
Last edited on Apr 6, 2011 at 9:31pm
Apr 6, 2011 at 9:28pm
Good point Branflakes.

As far as I am aware, the way the loops were previously written, they would just loop on and on until there happens to be a 'zero' in memory location &var+i, i.e. for example this code:
1
2
3
    for (int i = 0; bCheck[i]; i++) {
        bCheck[i] = false;
    }

loops until bCheck[i] is false, which is nothing to do with your program (it means the program will behave differently depending on what happens to be in memory around it...).
Apr 6, 2011 at 10:12pm
The condition that I use in my loops go untill there is an undefined value in the array. It works fine and I use it in my other programs without a problem.
Apr 6, 2011 at 10:26pm
The condition that I use in my loops go untill there is an undefined value in the array


Actually, it loops until you get a zero in the array. There is no "undefined" value. You will just keep going and going. You have loop until you hit whatever size you had for it:

1
2
3
for(unsigned int i = 0; i < array_size; ++i) {
    std::cout<<array[i];
}
Apr 7, 2011 at 6:39am
@firedraco

Presumably, however, looping using var_name[i] as a condition will work with string literals, since they have a null terminator.
Apr 7, 2011 at 12:02pm
Thanks for the help on that front, but none of these things would do much to help this program work. Is there anything you guys can do to help? I've made some revisions, but now it returns 15104. I have no Idea why.
Apr 7, 2011 at 8:43pm
See if this gives you what you need:

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
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <ctime>

// Write out a vector, one line per value.
void print(const std::vector<int>& v)
{
	std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));
}

// Generate a random sequence based on the size of the supplied vector.
void random_sequence(std::vector<int>& v)
{
    // Generate sequence.
    for (size_t i = 0; i < v.size(); i++)
    {
        v[i] = i;
    }
    // Randomize it.
    std::random_shuffle(v.begin(), v.end());
}

int main()
{
    std::srand(std::time(0));   // Initialize RNG.
    
    std::vector<int> v(10);     // Create a vector of 10 numbers.
    random_sequence(v);         // Create the random sequence.
    print(v);                   // Write the random sequence.

    return EXIT_SUCCESS;
}


If you really need to use arrays instead of vectors, then the code can be easily modified to use them.
Apr 7, 2011 at 11:11pm
Thanks a lot, it works great. Since I am just learning code, I don't really know vectors. How would random shuffle be used with arrays?
Apr 8, 2011 at 3:05am
Many of us think vectors should be taught before arrays. That said,
1
2
3
int array[size];
…
std::random_shuffle(array, array + size);
Apr 8, 2011 at 4:16am
Just wrote up a function that works perfectly. Thanks for the help everyone!
Topic archived. No new replies allowed.