Hi, i need help or advice on how to start a code i have to write. This is the details of the program with an example:
Any help or advice on how to go about this would be greatly appreciated.

Dani AI

Generated

Short summary and a few practical fixes tied to the thread (for , building on and ):

The reason the output became “smiley faces” is that the expression used a logical operator to combine two generators, which yields a boolean (0 or 1) — then that small number was interpreted as a character. Also watch the off-by-one: each alphabet has 26 letters, so use 26 as the range, not 25.

A clear, robust approach is: decide case first, then add a 0..25 offset. Using C++11 <random> avoids modulo bias and is easy to read:

#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> caseDist(0,1);
    std::uniform_int_distribution<int> offs(0,25);

    for (int i = 0; i < 10; ++i) {
        char c = (caseDist(gen) ? 'A' + offs(gen) : 'a' + offs(gen));
        std::cout << c;
    }
    std::cout << '\n';
}

Alternative: build a pool string of 52 letters and pick an index — simpler to reason about:

std::string pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::uniform_int_distribution<int> pick(0, pool.size() - 1);
char c = pool[pick(gen)];

If constrained to the old C RNG, the simplest correct expression is to pick case and offset separately (remember to seed the RNG once). Example conceptually: pick 0/1 for case, then add a 0..25 offset. Common pitfalls: do not use && to combine generators, use 26 not 25, and remember seeding.

Recommended Answers

All 8 Replies

You probably need to be more specific as to what you need to get any help.

Good advice if you're stuck is to turn of your screen and grab pen and paper and think it over until you know what you want to do. Then coding comes easier.

Get on with it! Deadline is on Monday! :P

yes i do! ok i have a question, how would you generate random upper and lower case characters (i.e. letter)?

You can use the rand function with the range of the ASCII characters you want and put the numbers into chars.

You can use the rand function with the range of the ASCII characters you want and put the numbers into chars.

could you show me an example of how you would write it out?

Ok, here's some links you should check out and below, a simple example.

Try it out, make changes, read the documentation and enjoy the endless possibilities it provides.
rand() function

srand() function (generates a random seed)

Time library from c where we can get the time_t time ( time_t * timer ); function

ASCII codes:
http://es.wikipedia.org/wiki/ASCII

#include <iostream>
//ctime is used to set the random seed.
#include <ctime> 

int main(int argc, char** argv)
{
  using namespace std;
  /*Initializes de seed to use the current time
  so that the numbers are different each run 
  of the application*/
  srand((unsigned)time(NULL));
  
  /*Check the ASCII code values.
  Lowercase goes from a = 97-> z=122 
  Uppercase goes from A = 65 -> Z=90 */

  //rand() returns a number between 0 and RANDMAX
  
  //arbitrarily, I decided to show 40 random letters
  for(int iii=0;iii<40;iii++)
  {
    char letter = rand()%25 + 65;
    cout<<letter<<" ";
  }

  /*
  Exercise 1: change 65 to 'A' 
  Exercise 2: change 65 to 'a'
  3: Use a number as a seed (instead of the time(NULL) function) 
  and you'll always get the same succession of random numbers"
  4: Test and research 
  */
}

Good luck and remember, be specific when you ask for help and show what you have done so far.

http://www.catb.org/esr/faqs/smart-questions.html

commented: nice +1

thanks a lot! but how would you get random upper and lower case letters at once?
ex output: cIyXv

Think about it. It's not hard and there's quite a few ways to accomplish it.

Before, you wanted 25 possible consecutive ASCII codes.

Now you want 50 (in groups of 25 and 25 starting from 'A' 65 and 'a' 97.

i was thinking maybe

char letter = rand()% 25 + 65 && rand% 25 + 97;

but i get smiley faces

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.