-3

Possible Duplicate:
How do I create a random alpha-numeric string in C++?

I need to create a 6 digit number. What should I use? Can someone give me a c++ code example?

This is my code: (once in a while the number is repeating)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int
main (int argc, char *argv[])
{
  /* Simple "srand()" seed: just use "time()" */
  unsigned int iseed = (unsigned int)time(NULL);
  srand (iseed);

  /* Now generate 5 pseudo-random numbers */
  int i;
  bool da=false;
  while (da==false)
  {if (rand ()%1000000<=999999)
  {cout<<"random nr: "<<rand ()%1000000<<endl;
  da=true;
}
else da=false;
}
 /* for (i=0; i<5; i++)
  {
    printf ("rand[%d]= %u\n",
      i, rand ());
  }*/
  return 0;
}

Thx Appreciate

Community
  • 1
  • 1
user1222905
  • 533
  • 2
  • 17
  • 36
  • i haven't ask anything like this before – user1222905 Mar 20 '12 at 14:58
  • 1
    But someone else has, if the duplicate question doesn't answer your question please edit this one to be clearer. – Konrad Mar 20 '12 at 14:59
  • @user1222905: you probably haven't used google or the search function here either. – Karoly Horvath Mar 20 '12 at 15:01
  • 1
    @juergend: In the accepted answer to that question `rand()` is suggested which [might not be secure enough](https://www.securecoding.cert.org/confluence/display/seccode/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers) for password generation. – hc_ Mar 20 '12 at 15:05
  • i've edited my code. please delete the -1 vote – user1222905 Mar 20 '12 at 15:09

3 Answers3

1

well you can do something like this

#include <random>
#include <string>

std::string s("      "); //six spaces
std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<char> dist('0', 'z');

for(char& c : s)
{
    c=dist(eng);
}

however it will include some punctuation. you can modify it to ignore character outside of your accepted range.

http://en.cppreference.com/w/cpp/numeric/random

http://www.asciitable.com/

EDIT: Merging mine and James answer together for a flexible solution could yeild something like this:

std::string const char_set(
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789" );

std::string s("      "); //six spaces
std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<std::size_t> dist(0u, char_set.size() - 1);

for(char& c : s)
{
   c=char_set[dist(eng)];
}  

EDIT: I changed the end of the distribution to size() - 1, otherwise the terminating null will be included in the distribution.

cvanbrederode
  • 372
  • 3
  • 11
111111
  • 15,686
  • 6
  • 47
  • 62
  • 1
    +1 I hadn't come across this way of doing it before, thanks for teaching me something new :-) – Konrad Mar 20 '12 at 15:03
  • @Konrad no problem the random number stuff is a great addition to the standard library in C++11 – 111111 Mar 20 '12 at 15:05
  • thank you bery much. What libraries do I have to include? I have errors: random_device is not a member of std – user1222905 Mar 20 '12 at 15:05
  • The characters in the password will depend on the encoding used locally; on an IBM mainframe, `'0'` is greater than `'z'`---I don't know what the `uniform_int_distribution` will do there. (There's also the fact that `std::uniform_int_distribution` isn't generally available.) – James Kanze Mar 20 '12 at 15:06
  • @JamesKanze I purposely left some scope for improvement in this algorithm because the rest is just platform semantics which the user can modify to their needs. On Ubuntu the OP distro the GCC version (4.5) has the random number stuff complete. – 111111 Mar 20 '12 at 15:11
  • include what libraries? I know I have to include libraries using #include. But I don;t know wha libraries – user1222905 Mar 20 '12 at 15:11
  • @user1222905 it is the standard library so as long as you are compiling with a proper C++ compiler like g++ it will be linked automatically. no extra libs. :D. you will need to ensure you can use these new components by specifying `-std=c++0x` on g++. – 111111 Mar 20 '12 at 15:13
  • errors: error: ‘random_device’ is not a member of ‘std’, generate_nr.cpp:13: error: ‘mt19937’ is not a member of ‘std’, generate_nr.cpp:14: error: ‘uniform_int_distribution’ is not a member of ‘std’, error: expected primary-expression before ‘char’ – user1222905 Mar 20 '12 at 15:16
  • @user1222905 what compiler are you using and what version is it (run it with --version). you have #include at the top right? – 111111 Mar 20 '12 at 15:20
  • yes. /usr/include/c++/4.4/c++0x_warning.h:31: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. – user1222905 Mar 20 '12 at 15:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9096/discussion-between-user1222905-and-111111) – user1222905 Mar 20 '12 at 15:26
  • I've solved the problem. I had to include the random_device.cpp in my compilation method. I would still like to ask you how to solve the problem if I want to have a lenght of 6 characters with no space? – user1222905 Mar 21 '12 at 10:26
  • collision rate is around 2.5% per 100M entries. – kagali-san Jul 26 '14 at 23:58
0

Put all of your digits in a Vector

int lowest_value = 0, highest_value = VECTOR_SIZEOF, range = (highest_value - lowest_value) + 1;
int pass_length = 6;
int random_number = 0;
string password = "";

for (i=1; i<pass_length; i++) 
{
    random_number = lowest_value + (int)((range * rand()) / (RAND_MAX + 1.0));
password.insert((char)random_number);
}
Yeppao
  • 130
  • 2
  • 12
0

Just use the same technique you'd do for any random choice in a set of finite values. Put the values in an array, and use the random generator to choose from them. Something along the lines of:

std::string const legalChars(
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "0123456789" );
std::string results;
while ( results.size() != targetLength ) {
    results += legalChars[ randomInt( legalChars.size() ) ];
}

(randomInt should be a function which returns a random integer in the range [0,arg).)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • what is arg. Can you also post the method randomInt? – user1222905 Mar 20 '12 at 15:17
  • `arg` is an argument to `randomInt`. `randomInt` is a placeholder for whatever function you are using to return a random integer in the given range. `dist`, for example, in 111111's solution. I didn't bother to expand it because there are so many possibilities, and one from the new standard library is to be preferred, if your compiler supports it. – James Kanze Mar 20 '12 at 17:12