6 Replies - 535 Views - Last Post: 30 March 2011 - 08:47 AM Rate Topic: -----

#1 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

C++ Random Code Generator

Posted 29 March 2011 - 07:39 AM

I'm making a tiny program to familiarize myself with arrays. My code is this:
#include <iostream>
#include <string.h>
#include <time.h>

using namespace std;
void generate();

int rnd(int range);

int main() {
    int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, i = 0;
    char y[4] = {'A', 'B', 'C', 'D'};
    
generate();

while (i < 3) {
     void generate();
     i++;
     }
     
     getchar();
     return 0;
     }

    int rnd(int range) {
      int r;
      r = rand() % range;
      return (r);
            }
                
    void generate() {
      int num, letter;
      
      num = rnd(10);
      letter = rnd(4);
      
      cout << letter << num << endl;
      
      srand((unsigned)time(NULL));
                    }




But it generates two numbers (71, for example, when I'm looking for something like B5 or D3) and only does it once when I want three. What am I doing wrong?

This post has been edited by muffinman8641: 29 March 2011 - 07:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Random Code Generator

#2 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Re: C++ Random Code Generator

Posted 29 March 2011 - 07:55 AM

OK, I think it has something to do with lines 32-35, where I declare num and letter as random numbers. How can I make letter the random letter set?

Also, I turned the Y variable in int main into "char y[]= "ABCD";"

This post has been edited by muffinman8641: 29 March 2011 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 runfaster  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 135
  • Joined: 23-January 09

Re: C++ Random Code Generator

Posted 29 March 2011 - 08:06 AM

What you are doing currently is printing out the random numbers generated by rnd(). You need to tell the computer how to apply those numbers.

Something along the lines of:

cout << x[num] << y[letter] << endl; 


To do this, you would need to either make the x and y arrays global or pass them into the generate function.

Also be aware that you'll probably experience an out of bounds exception when you get a 10 or a 4. Arrays have spaces labeled 0-(n-1) for a an array of size n. I could be wrong. I forget exactly how rnd() works.

Edit: fixed a few grammar things

This post has been edited by runfaster: 29 March 2011 - 08:07 AM

Was This Post Helpful? 1
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: C++ Random Code Generator

Posted 29 March 2011 - 08:07 AM

You are displaying two numbers here:
cout << letter << num << endl;


That is why you see 2 numbers. letter is an index for your y array. So you need to do this :

cout << y[letter] << x[num] << endl;

This post has been edited by chinchang: 29 March 2011 - 08:07 AM

Was This Post Helpful? 1
  • +
  • -

#5 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Re: C++ Random Code Generator

Posted 29 March 2011 - 03:51 PM

Thanks! I have the right combo of letters and numbers now, but it only does one instead of three.


#include <iostream>
#include <string.h>
#include <time.h>

using namespace std;
void generate();

int rnd(int range);

int main() {
    int i = 0;
    system("CLS");
    system("color 0E");
    
generate();

while (i < 3) { 
     void generate();
     i++;
     }
     
     getchar();
     return 0;
     }


    
    
    int rnd(int range) {
      int r;
      r = rand() % range;
      return (r);
            }
                
    void generate() {
      int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, num;
      char y[]= "ABCD", letter;
      
      num = rnd(10);
      letter = rnd(4);
      
      cout << y[letter] << x[num] << endl;
      
      srand((unsigned)time(NULL));
                    }




Not a big deal, but I can't see why.

Also, I now decided to expand it a little to make a random password generator (like the ones you get when you make a new account somewhere; 8GX224L6 or something like that). The thing is, if I just keep repeating << x[num] or y[letter], it gives the same one. How can I seed new random number for each character?

This post has been edited by muffinman8641: 29 March 2011 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: C++ Random Code Generator

Posted 30 March 2011 - 05:30 AM

Line 18:
void generate();



This is not the way you call a function. You are only declaring a function. The only time the generate() function is called is on Line 15. That is why you get only 1 output.

Change Line 18 to:
generate();




And for generating a more random number, you provide a seed value to srand(). See here.

This post has been edited by chinchang: 30 March 2011 - 05:32 AM

Was This Post Helpful? 2
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,523
  • Joined: 08-August 08

Re: C++ Random Code Generator

Posted 30 March 2011 - 08:47 AM

I would only call srand() once and at the beginning of the code. All it does is initialize the random number seed, and there's no need to do that repeatedly.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1