real random ...

May 5, 2009 at 6:27am
Hi all. I am using the random function, but the resaults are no good. It gives 111111111111111 or 2222222222222222 etc...
There is a real random generator?

1
2
3
4
5
6
int random(int i)
{
        /* initialize random seed: */
        srand((unsigned)time(NULL));
        return rand() % i+1;
}
May 5, 2009 at 7:39am
closed account (z05DSL3A)
srand((unsigned)time(NULL)); should only be called once in your program.
May 5, 2009 at 9:39am
Yes i know that, but how can i do that? Maybe in the beginning of main.cpp i can a call to srand and then in random don't have srand at all?
Also there is the srand(rand()*GetTickCount()); but this in visual studio. I use GCC and codeblocks. I GetTickCount() for standard c++?

Also i found all this ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdlib.h>
#include <string.h>
#include <time.h>

//use this first function to seed the random number generator,
//call this before any of the other functions
void initrand()
{
    srand((unsigned)(time(0)));
}

//generates a psuedo-random integer between 0 and 32767
int randint()
{
    return rand();
}

//generates a psuedo-random integer between 0 and max
int randint(int max)
{
    return int(max*rand()/(RAND_MAX+1.0));
}

//generates a psuedo-random integer between min and max
int randint(int min, int max)
{
    if (min>max)
    {
        return max+int((min-max+1)*rand()/(RAND_MAX+1.0));
    }
    else
    {
        return min+int((max-min+1)*rand()/(RAND_MAX+1.0));
    }
}

//generates a psuedo-random float between 0.0 and 0.999...
float randfloat()
{
    return rand()/(float(RAND_MAX)+1);
}

//generates a psuedo-random float between 0.0 and max
float randfloat(float max)
{
    return randfloat()*max;
}

//generates a psuedo-random float between min and max
float randfloat(float min, float max)
{
    if (min>max)
    {
        return randfloat()*(min-max)+max;    
    }
    else
    {
        return randfloat()*(max-min)+min;
    }    
}

//generates a psuedo-random double between 0.0 and 0.999...
double randdouble()
{
    return rand()/(double(RAND_MAX)+1);
}

//generates a psuedo-random double between 0.0 and max
double randdouble(double max)
{
    return randdouble()*max;
}

//generates a psuedo-random double between min and max
double randdouble(double min, double max)
{
    if (min>max)
    {
        return randdouble()*(min-max)+max;    
    }
    else
    {
        return randdouble()*(max-min)+min;
    }
}

char* randstr(char* str, unsigned long length)
{
    //make we were passed a valid pointer
    if (!str)
    {
        return 0;
    }

    //make sure the string is supposed to contain something
    if (!length)
    {
        return 0;
    }

    //put random characters into the string, give both
    //upper and lower case numbers an equal chance at
    //being used
    for (unsigned long x=0;x<length-1;x++)
    {
        if (!randint(1))
        {
            str[x]=(char)randint(65,90);
        }
        else
        {
            str[x]=(char)randint(97,122);
        }
    }

    //null terminate the string
    str[x]=0;

    return str;
}

//flags for the extended randstr function
#define RAND_STR_CAPITOL_LETTERS    0x00000001    //65 through 90
#define RAND_STR_LOWER_CASE_LETTERS    0x00000002    //97 through 122
#define    RAND_STR_NUMBERS        0x00000004    //48 through 57
#define RAND_STR_SYMBOLS_1        0x00000008    //32 through 47
#define RAND_STR_SYMBOLS_2        0x00000010    //58 through 64
#define RAND_STR_SYMBOLS_3        0x00000020    //91 through 96
#define RAND_STR_SYMBOLS_4        0x00000040    //123 through 126
#define RAND_STR_NON_PRINTING        0x00000080    //1 through 31 and 127
#define RAND_STR_EXTENDED_ASCII        0x00000100    //128 through 255

char* randstr(char* str, unsigned long length, unsigned long flags)
{
    //make we were passed a valid pointer
    if (!str)
    {
        return 0;
    }

    //make sure the string is supposed to contain something
    if (!length)
    {
        return 0;
    }

    //if none of the flags were used then we set flags to use just upper and lower case
    if ((!flags&0x00000001)&&(!flags&0x00000002)&&(!flags&0x00000004)&&
    (!flags&0x00000008)&&(!flags&0x00000010)&&(!flags&0x00000020)&&(!flags&0x00000040)
    &&(!flags&0x00000080)&&(!flags&0x00000100))
    {
        flags=RAND_STR_LOWER_CASE_LETTERS|RAND_STR_CAPITOL_LETTERS;
    }

    char t[9]={0};
    int i=0;

    //each pass of the loop the flags are checked and for each flag that is used
    //then a random character in the flags range is added to a temporary array of
    //characters, after all the flags are checked a random character from the
    //temporary array is chosen to be inserted into the string
    for (unsigned long x=0;x<length-1;x++)
    {
        i=0;

        if (flags&0x1)
        {
            t[i]=randint(65,90);
            i++;
        }

        if (flags&0x2)
        {
            t[i]=randint(97,122);
            i++;
        }

        if (flags&0x4)
        {
            t[i]=randint(48,57);
            i++;
        }

        if (flags&0x8)
        {
            t[i]=randint(32,47);
            i++;
        }

        if (flags&0x10)
        {
            t[i]=randint(58,64);
            i++;
        }

        if (flags&0x20)
        {
            t[i]=randint(91,96);
            i++;
        }

        if (flags&0x40)
        {
            t[i]=randint(123,126);
            i++;
        }

        if (flags&0x80)
        {
            if (!randint(32))
            {
                t[i]=127;
                i++;
            }
            else
            {
                t[i]=randint(1,31);
                i++;
            }
        }

        if (flags&0x100)
        {
            t[i]=randint(127,255);
            i++;
        }

        str[x]=t[randint(i-1)];
    }

    //null terminate the string
    str[x]=0;

    return str;
} 


May 5, 2009 at 10:09am
closed account (z05DSL3A)
Yes i know that, but how can i do that? Maybe in the beginning of main.cpp i can a call to srand and then in random don't have srand at all?

Yes you can call srand at the start of the main function either directly or via a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <time.h>

int random(int i)
{
    return rand() % i+1;
}

int main ()
{
    using std::cout;
    using std::endl;

    /* initialize random seed: */
    srand((unsigned)time(NULL));

    for (int i=0; i < 10; ++i)
    {
        cout << random(50) << " : ";
    }
    
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <time.h>


void initrand()
{
    srand((unsigned)(time(0)));
}

int random(int i)
{
    return rand() % i+1;
}

int main ()
{
    using std::cout;
    using std::endl;

    /* initialize random seed: */
    initrand();

    for (int i=0; i < 10; ++i)
    {
        cout << random(50) << " : ";
    }
    
    return 0;
}


There is a real random generator?

Can you elaborate on this a bit? Do you require a number in the set of Real numbers, or do you mean real as apposed to pseudo random number, or something else?


Last edited on May 5, 2009 at 10:14am
May 5, 2009 at 11:09am
When looking in net, the trully real number, generation is not easy. For my needs all tha are ok and thank's for help. It is a filosophique question. For example there is a project in sf.net in real random from sound card noice.
Jim
Topic archived. No new replies allowed.