2013-12-12 71 views
4

我想创建随机数据进行测试。我想用字母'A'填充100个随机长度的字符串。n次追加一个字符

例如:

array[0] = "AAAAA" 
array[1] = "AAAAAAAA" 
array[2] = "A" 
... 

char **create_string() 
{ 
    char **array = malloc(sizeof(**array)); 

    srand((unsigned int)time(NULL)); 
    int random = 0; 

    int i, j; 
    for(int i=0; i<100; i++) 
    { 
     random = rand() % 100; 
     for(j=0; j < random; j++) 
     { 
      array[i] = // some sort of string append that would be cheap. 
     } 
    } 
} 

我看着这个C string append和他们使用strcat。有没有更好的方法来解决我的问题?因为我将在循环中运行以创建这些随机大小的字符串。

回答

3
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
char **create_string(size_t n) { 
    char **array = malloc(sizeof(char*) * n); 
    int i, j; 
    for(i=0; i<100; i++) 
    { 
     size_t sz = rand() % 100; 
     array[i] = malloc(sz + 1); 
     for(j=0; j < sz; j++) { 
      array[i][j] = 'A'; 
     } 
     array[i][sz] = 0; 
    } 
    return array; 
} 

int main() { 
    char **array; 
    size_t i; 
    srand((unsigned int)time(NULL)); 
    array = create_string(100); 
    for (i = 0; i < 100; i++) 
     printf("%s\n", array[i]); 
    return 0;  

} 

或者,你可以创建一个模板字符串和复制所需的字符数到每一个随机字符串:

char **create_string(size_t n) { 
    char template[101]; 
    char **array = malloc(sizeof(char*) * n); 
    int i; 

    for (i = 0; i < 100; i++) 
     template[i] = 'A'; 
    template[100] = 0; 
    for(i = 0; i < n; i++) { 
     size_t sz = rand() % 100; 
     array[i] = malloc(sz + 1); 
     strncpy(array[i], template, sz); 
     array[i][sz] = 0; 
    } 
    return array; 
} 
+0

为什么你不使用srand()函数中的第二个? – noufal

+0

@noufal,在应用程序中对srand的一次调用通常就足够了,并且在两种情况下都在主函数中调用它。 – perreal

+1

第二种方法比第一种方法快近10倍... – noufal

0

这将取决于你想要的字符串长度的分布。这是字符串长度的均匀分布,从0到200

int n = rand() % 200 * sizeof(*array); 
array[i] = malloc(n + 1); 
memset(array[i], 'A', n); 
array[i][n] = '\0'; 

但你可以有一个高斯分布,泊松分布等

0
char **create_string() 
    { 
    char **array = malloc(sizeof(char *) * 100); 

      srand((unisgned int)time(NULL)); 

     int i; 
     for (i = 0; i <100;i++) 
     { 
      int random = rand() % 100; 
      array[i] = malloc(random); 
      memset(array[i],'A',random-1); 
      array[random-1] = '\0'; 
     } 
     return array; 
    } 

问题为您解决:发生了什么如果随机是0?此外,随机数不会是平均分布的。只有2的幂模将实现这一点。

0

下面是一个不把上限在任何一个字符串的方法,但确实将所有字符串的总长度绑定了确切的。它也只能拨打malloc两次。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define TOT_SIZE (5000) /* adjust to taste */ 
#define TOT_STRS (100) 

char **create_strings() 
{ 
    char **array = (char**) malloc(TOT_STRS * sizeof(char *)); 
    char *str = (char*) malloc(TOT_SIZE); 
    int zeros = 1; 
    int i; 

    memset(str, 'A', TOT_SIZE - 1); 

    str[TOT_SIZE - 1] = 0; 

    while (zeros < TOT_STRS) 
    { 
     int pos = rand() % TOT_SIZE; 

     if (str[pos] != 0) 
     { 
      str[pos] = 0; 
      zeros++; 
     } 
    } 

    array[0] = str; 

    for (i = 1; i < TOT_STRS; i++) 
    { 
     array[i] = array[i - 1] + strlen(array[i - 1]) + 1; 
    } 

    return array; 
} 

和短的测试程序:

int main() 
{ 
    char **a = create_strings(); 
    int i; 

    for (i = 0; i < TOT_STRS; i++) 
    { 
     printf("%3d: %s\n", i, a[i]); 
    } 

    return 0; 
} 

此代码假定所有的随机字符串需要是不重叠的。如果它们可以在内存中重叠,则只需要一个字符串和一个指向该一个字符串中不同起点的指针数组。

0

您不必分配真正的100个字符串。首先,你只需声明一个足够长的数组char long_array[100]。然后你用random = rand() % 100;得到随机数。其次,您只需将long_arrayrandom传递给您的函数。那么你的问题就解决了。

0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <time.h> 

char **create_string(const size_t array_size, 
       const size_t string_size, 
       const unsigned char chr) 
{ 

    srand((unsigned)time(NULL)); 
    char ** array = malloc(array_size * sizeof (char *)); 

    size_t t; 

    for (t = 0; t < array_size; ++t) { 
     array[t] = malloc(string_size * sizeof(char)); 
     array[t][string_size] = '\0'; 
     memset(array[t], chr, (rand() % string_size) + 1); 
    } 

    return array; 
} 

main() { 

    char ** randstring = create_string(10, 7, 'A'); 
    int t = 0; 
    for (; t < 10; ++t) 
     printf("%s\n", randstring[t]); 
    return 0; 
} 

可能的输出

AAAAAA 
AAAAA 
AAAAAA 
AAA 
AAAAA 
AAAAAA 
AAAAAA 
AAAAAAA 
AAAA 
AAAA 
+0

您的字符串不是\ 0终止的 –