Creating Random Strings: Hash Functions & Beyond

AI Thread Summary
Hash functions are designed to take an input string and produce a fixed-size output, known as a hash, which represents the original string. They are not intended for generating random strings. Instead, to create random strings, a random byte generator is required. Common methods for generating random strings include using the system time as a seed for a pseudo-random number generator. In C, this can be implemented by including the necessary libraries and using functions like `srand()` and `rand()`. An example code snippet demonstrates how to generate a random string of capital letters based on a specified length. The program initializes the random number generator with the current time and fills an array with random characters, which are then printed as the output. This approach effectively illustrates the process of creating random strings in programming.
chaoseverlasting
Messages
1,050
Reaction score
3
What exactly are hash functions and how do you use them to create a random string? What other ways are there to create random strings.
 
Technology news on Phys.org
http://en.wikipedia.org/wiki/Hash_functions

One does not implement hash functions to produce a random string. Hash functions take a string as an argument and output a string of a smaller size which is a hash that represents the original string. An example of a hash function would be the famous DES.

If you wish to produce a random string and not what a hash function does, you need a random byte generator. There are many methods of random generators. A common implementation of a random generator is to take the value of the time, and do a manipulation on it. Once you have a random byte generator, you can produce the number of bytes you want to produce a random string.
 
Last edited:
How would you get the system time in c++ and use it to create a random string?
 
I am not a C programmer but it seems to me you'd use the pseudo random number generator and seed it with the time:



Code:
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
srand(time(0));
 
chaoseverlasting said:
How would you get the system time in c++ and use it to create a random string?

Here is an example of a random string generator written in C. It generates a string of random capital letters.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(int argc, char *argv[])
{
        if(argc < 2){
                printf("USAGE: %s [Length of string]\n", argv[0]);
                return 0;
        }

        int length      = atoi(argv[1]);
        char *randstr   = malloc(length + 1);
        randstr[length] = '\0';

        time_t ltime = time(NULL);
        srand(ltime);

        int i;
        for(i = 0; i < length; i++)
                randstr[i] = (rand() % 26) + 65;

        printf("%s\n", randstr);
        return 0;
}

Here is an example of the output:

Code:
$ ./randstr
USAGE: ./randstr [Length of string]
$ ./randstr 20
MTZEKHHTQJGPDNHBNSTN
$ ./randstr 20
IEMBTDVFDEDMFWXEYESX
$ ./randstr 20
EMCYCCJOTCAJHCNHMQOH
$
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top