Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Random ints and chars in c++


  • Please log in to reply

#1
MaverickSidewinder

MaverickSidewinder

    Member

  • Member
  • PipPipPip
  • 257 posts
Hey, what do i have to do to tell my c++ program to generate some random integers (ints) or characters (chars i.e. "a" "b" ecc)...Also, how do i give it limitations? Like say for example:

Insert the maximum number: (a)
Insert the minimum number: (b)
And c is smaller or equal to a (<=) or it is bigger or equal than b (>=)...

That above is for numbers, but lets say i wanted to make a random letter generator (for passwords)...which includes whole numbers (ints) and characters...how do i do it? Thanks for your time! :tazz:
  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
There is a function for this in the C standard library (there may be a C++ version too, you'd have to look).

#include <stdlib.h>
void srand(unsigned int seed); // seeds the random number generator
int rand (void); // returns a random number in the range 0 to RAND_MAX.

If you want a random number that falls within a certain range (e.g. character codes), be inventive! The following would place a random (lowercase) letter into char c (I initialize the random number generator with the current clock time, which is a good pseudo-random initialization vector):

#include <stdlib.h>
#include <time.h>

char c;
srand( (unsigned int) time(NULL));
c = (rand() % 26) + 'a';


See how this works? It generates a random integer, takes its modulo 25 (so that it becomes an integer from 0 to 25), and then adds that value to the ASCII representation of the letter 'a'. That way you will always be guaranteed to get a value between 'a' and 'z'.

See the GNU C documentation:
http://www.gnu.org/s...html#ISO-Random
http://www.delorie.c...c/libc_682.html
  • 0

#3
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
On second thought, it really depends on *how* random you want the numbers to be. Some implementations of random number generators are much better than others, and I don't think the C one is terribly good.
  • 0

#4
MaverickSidewinder

MaverickSidewinder

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 257 posts
Thank you SwanDog for you reply and time...i really appreciate it...

I'll look into it! :tazz:
  • 0

#5
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Happy to help :tazz:
  • 0

#6
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

There is a function for this in the C standard library (there may be a C++ version too, you'd have to look).

#include <stdlib.h>
void srand(unsigned int seed); // seeds the random number generator
int rand (void); // returns a random number in the range 0 to RAND_MAX.

If you want a random number that falls within a certain range (e.g. character codes), be inventive! The following would place a random (lowercase) letter into char c (I initialize the random number generator with the current clock time, which is a good pseudo-random initialization vector):

#include <stdlib.h>
#include <time.h>

char c;
srand( (unsigned int) time(NULL));
c = (rand() % 26) + 'a';
See how this works? It generates a random integer, takes its modulo 25 (so that it becomes an integer from 0 to 25), and then adds that value to the ASCII representation of the letter 'a'. That way you will always be guaranteed to get a value between 'a' and 'z'.

See the GNU C documentation:
http://www.gnu.org/s...html#ISO-Random
http://www.delorie.c...c/libc_682.html

This is a good idea but you forgot one thing. (rand() % 26) + 'a' will give you an integer value. You would want to cast it to a char:
c = (char) (rand() % 26 + 'a');
If you want to include numbers and capital letters too, you could do something similar to this:
#include <iostream>
#include <ctime>

using namespace std;

int main() {
	srand((unsigned) time(NULL));
	
	const int passLen = 10;
	for (int i = 0; i < passLen; i++) {
		cout << (char) (rand() % ('z' - '0' + 1) + '0');
	}
}
On the ASCII table, out of numbers, lower case letters and uppercase letters, numbers have the lowest ASCII value, then capital letters, then lowercase letters. So the lowest we would want is '0', and the highest we would want is 'z'.

Hope this helps!
  • 0

#7
MaverickSidewinder

MaverickSidewinder

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 257 posts

This is a good idea but you forgot one thing. (rand() % 26) + 'a' will give you an integer value. You would want to cast it to a char:

c = (char) (rand() % 26 + 'a');
If you want to include numbers and capital letters too, you could do something similar to this:
#include <iostream>
#include <ctime>

using namespace std;

int main() {
	srand((unsigned) time(NULL));
	
	const int passLen = 10;
	for (int i = 0; i < passLen; i++) {
		cout << (char) (rand() % ('z' - '0' + 1) + '0');
	}
}
On the ASCII table, out of numbers, lower case letters and uppercase letters, numbers have the lowest ASCII value, then capital letters, then lowercase letters. So the lowest we would want is '0', and the highest we would want is 'z'.

Hope this helps!




Thank you very much!
  • 0

#8
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Crap. How can I delete a post? Or can someone delete this for me?

Edited by destin, 26 February 2006 - 09:28 AM.

  • 0

#9
MaverickSidewinder

MaverickSidewinder

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 257 posts
admin preferred to keep all posts and threads...so for now...all you can do is edit :tazz:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP