dimanche 7 juin 2020

A random function freezing even when given correct arguments

Here's the program that I'm trying to run:

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<string>
using namespace std;
int Generate_random(int a, int b)
{
    int n=b-a+1;
    int packet=RAND_MAX/n;
    int r;
    do r=rand()/packet;
    while(r<a || r>b);
    return r;
}
string Generate_Password(int len)
{
    string pw="";
    int least='a';
    int highest='z';
    for(int l=0; l < len ; ++l)
    {
        char c= Generate_random(least, highest);
        pw += c;
    }
    return pw;
}
int main()
{
    srand(time(0));
    cout<<Generate_Password(8);
}

It must be clear that I'm trying to generate a random character password (from 'a' to 'z') of 8 characters long; And the function Generate_random() doesn't works right. I tried running some trials and found that It's getting freezed just because i passed least and highest(integers which are obtained from characters) I dont know how to explain this .Also , how to make it happen? Thanks for the help.




Aucun commentaire:

Enregistrer un commentaire