Hello,
I am using a code to generate random numbers upto 32 char...
The problem is that i want to display the 32 characters each randomly because the code is displaying the same char 32 times and i want to get the 32 chars generated randomly...
The code is:-

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
char RandomNumber()
{
//int counter = 0;
char a,b,c, random_char;
a='a';b='0';
//for (counter = 0; counter <1; counter++)
//{
srand(time(NULL));
random_char= (char) rand();
c=a+(random_char%b);
//cout <<c;
//}
return(c);
}

int main()
{
	for(int j=0;j<32;j++){
	char e;
	e = RandomNumber();
	cout<<e;}
return 0;
}

The output i am getting is: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
and i want to get the 32 char different that is generated randomly each...
E.g: djh4652ghydfb45863mhggh4236muh2p

put the srand(time(NULL)); in the main(), such that it is run only once.

As you have it, every time RandomNumber() is called, srand() is seeded with the same value - hence rand() returns the same value each time.

commented: Good answer. +15

Hello,
Thanks dougy83...That works... thnks for the help..
The modified code is as follows:-

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

char RandomNumber()
{
char a,b,c, random_char;
a='a';b='0';
random_char= (char) rand();
c=a+(random_char%b);
return(c);
}

int main()
{
	srand(time(NULL));
	for(int j=0;j<32;j++){
	char e;
	e = RandomNumber();
	cout<<e;}
return 0;
}

The output is as follows: %cvff';q12582$^opl142[]c!m4fp6-|

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.