Creating Random Strings: Hash Functions & Beyond

Click For Summary

Discussion Overview

The discussion centers on the concept of hash functions and their role in generating random strings, as well as alternative methods for creating random strings. It includes technical explanations and practical coding examples, particularly in C++.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant questions the nature of hash functions and their application in generating random strings, seeking clarification on their purpose.
  • Another participant asserts that hash functions do not produce random strings but rather generate a hash that represents the original input string, suggesting the use of random byte generators instead.
  • A participant inquires about how to obtain the system time in C++ for the purpose of creating a random string.
  • A response suggests using a pseudo-random number generator seeded with the system time, providing a code snippet as an example.
  • Another participant provides a complete C program that generates a random string of capital letters based on user-defined length, demonstrating the use of system time and random number generation.
  • The example output of the program is shared, showing various random strings generated upon execution.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between hash functions and random string generation methods, but there is no consensus on the best approach to implement random string generation in C++.

Contextual Notes

Some limitations include the reliance on the randomness of the pseudo-random number generator and the assumptions made about the system time's effectiveness as a seed.

chaoseverlasting
Messages
1,051
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:

Similar threads

  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 10 ·
Replies
10
Views
5K
Replies
4
Views
6K
  • · Replies 21 ·
Replies
21
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
55
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K