my rand still outputing wrong numbers

Jul 8, 2013 at 6:32pm
my output still has the repeating numbers. i am pretty sure everything is right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       size = 10
    
    srand((unsigned)time(NULL));
    
    for (int i=0; i<size;i++){
        a[i] = (rand()%59)+1;
           if (a[size]== b[i]){
              for (int i=0; i<size;i++){
                 a[i] = (rand()%1000)+1;
              }
           }
    }
                   
     
}
Last edited on Jul 8, 2013 at 8:41pm
Jul 8, 2013 at 6:45pm
FYI: the random number generator that comes with the compiler is not really random enough.
you have two nested loops, and if size is big enough, you can run out of new random numbers after some iterations of the outer loop.
secondly, i can't see why you need the inner loop, the outer is sufficient.
and i can't understand the purpose of the condition in the if statement.
i might be able to help out if you clear your intentions in this code.
Jul 8, 2013 at 6:46pm
closed account (Dy7SLyTq)
try time(NULL) or the c++11 random header
Jul 8, 2013 at 8:12pm
i am trying to have 6 random numbers that are non repeating.
Jul 8, 2013 at 8:17pm
if i may change your code a bit:

1
2
3
4
    for (int i=0; i<size;i++){
        srand((unsigned)time(0));
        lotteryNum[i] = (rand()%59)+1;
    }


this shall make it more random.

EDITED: i had a mistake in the code, now fixed.
Last edited on Jul 8, 2013 at 8:20pm
Topic archived. No new replies allowed.