Help with random number generator

Apr 15, 2013 at 3:42pm
I am trying to have a program that generates numbers between 1 and 9 and wont repeat any numbers.

This is the error I keep getting
D:\Programming\C++ side Projects\random number\main.cpp|14|error: expected ';' before ')' token|

It runs if i take out the expression index!=not_it[1,2,3,4,5,6,7,8];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
7.  int main()
8. {
9.    int not_it[8] {1,2,3,4,5,6,7,8} , run = 0;
10.    while (run < 10)
11.    {
12.    srand((unsigned)time(0));
13.    int random_integer;
14.    for(int index=1; index<9; index++; index!=not_it[1,2,3,4,5,6,7,8]; ){
15.    random_integer = (rand()%10)+1;
16.    cout << random_integer << endl; }
17.
18.    run++;
19.    not_it[run] = random_integer;
20. }
21.
22. }
Apr 15, 2013 at 3:49pm
Your for loop has five sections. A normal for loop has only three sections.
Apr 15, 2013 at 4:09pm
LOL, I spy with my little eye a C++11 feature!
Apr 15, 2013 at 4:26pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <random>
#include <ctime>
#include <iostream>


int main()
{
    int numbers[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;

    // generate random numbers in [1,9] without repetion
    std::shuffle( numbers, numbers+9, std::mt19937( std::time(nullptr) ) ) ;
    for( int i : numbers ) std::cout << i << ' ' ;
}
Topic archived. No new replies allowed.