Randomize without repition

so i've tried and tried and tried and changed my code about ten times in the last hour, its getting really frustrating, so i decided i'll sign up and ask for help =( (feel like i failed myself) help please, here's the code:
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

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <iostream>



int main()
{
      char name[15];
      char position[10];
      char password[7];
      char password1[]="z3xn4ct";    
      char lot;
      int x;
      int y;
      int n;
      int counter;
      int aspace[51];
      using namespace std;
      bool check;
      
     time_t seconds;
     time(&seconds);
     srand((unsigned int) seconds);
    
     cout << rand()% 50 + 1 << endl;
     
     
      printf("\tWelcome TO P-SAS!\n NixGen's Parking Space Allotment Software\n");
      printf("Please Enter Name and Work Position\n");
      scanf("%s%s",name ,position); 
      for (counter=0; counter <3; counter++) {
      printf("Please Enter Password\n");
      scanf("%s",password);
      
      if (strcmp(password,password1)==0) //verify password
      {
                                        
                                       printf("Password Accepted!\n");
                                       break;
                                       }
                                       
                                       else  {
                                             printf("Incorrect Pass!\n");
                                             counter = 0;
                                             }
                                             }
  
           // for ( x = 0; x < 10; x++ ) {
    
      //aspace[x] = 0; /* Set each element to a value */
 // }
  
  printf( "Please Press the letter corresponding with the lot in which you wish to dock a vehicle:\n"
  "Lot A - Press A  Lot B - Press B  Lot C - Press C\n" );
  for (y = 0; y < 50; y++) {
         
      do
      {  
          n=rand() % 50 + 1;
          check=true;   
  for ( x = 0; x < y; x++ ) 
      if (n == aspace[y]) {
                 
      check=false; //set check to false
            break; //no need to check the other elements of value[]
        }
    } while (!check); //loop until new, unique number is found
      aspace[y]= n;
      printf("%d",aspace[y]);
      printf( "\n" );
      
      }
     
    if (x==50) {
          printf("Lot %s has reached its maximum capacity",lot);
          }         

getch();
}

[code]
[/code]
Last edited on
Hi. If your goal is to get random numbers without repeating numbers then you should consider too things: 1) how big is the space between minimum and maximum number the generator can give you; 2) how many of the numbers in the range are you going to use.

In case you are going to use, I would say, less than half of the numbers then you could store all generated numbers in some STL container/array and check for the presence of a new generated number, if a new generated number exists then you will generate a new one, otherwise you will store and use it.

In case the space between minimum and maximum number you can get isn't very big then you could store all possibilities in a STL container/array and get numbers from it and remove the number after you have got it.

First approach uses less memory, the second one uses less time.
Topic archived. No new replies allowed.