Random Numbers I Fail

Well I want to generate random numbers. The problem is, I don't want them to repeat, and I want to get numbers with in range of 1-100.
for example


..

ok so I have to create an array and made a loop.

this is what i wrote

#include <Windows.h>
#include <iostream>
#include <cstdlib>

int RandomShapes();

int main()
{
RandomShapes();
system("pause");
return 0;
}

int RandomShapes()
{
int RandNum;

for (int s = 0; s<=6; s++)
{
RandNum = rand() % 3 + 2;
int RandomShape[s] ;
RandomShape[s] = RandNum;
std::cout << s << "\n";

}
return 0;
}


but it gives me numbers in order from 0 - 6
why it isn't random like 3 4 2 1 5 6 0 ?

note that this code will be in window, and include <list> or <vector> don't work not sure what they do, but some of the people gave me examples with them. They do work in text command but not in window.
thx :D
appreciate your time

also i use dev c++ bloodshed
Last edited on
Here is an example where i use a list to store the numbers 1-100.
The Idea is, that i have a 'pool' from where i just randomly determine which position i erase.
Like this you just have to determine 1-100 first and then 1-99 and then 1-98 and you still keep your randomness how you like to do it.
What kind of storage you use, (if vector, or array or similar) is up to you. The idea is shown below.

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
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <list>
using namespace std;

int main()
{
    const int Count = 100;
    list<int> pool;
    srand(time(NULL));
    
    /* just create all numbers 1 - 100 */
    for (int i = 0; i < Count; i++)
        pool.push_back(i + 1);
    /* now generate 100 random positions from where you get the numbers */
    for (int i = 0; i < Count; i++) {
        int randPos = rand() % pool.size();
        list<int>::iterator it = pool.begin();
        advance(it, randPos);
        cout << *it << " ";
        pool.erase(it);
    }
    cout << endl;
}
You could generate an array of 100 elements from 1 to 100 (with a simple for loop) and then call random_shuffle (see http://www.cplusplus.com/reference/algorithm/random_shuffle/ )
i tried this, and it says list undeclared, i did include list, maybe its because the program is in window not text console based, how do i do in window then?
Topic archived. No new replies allowed.