# random function

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-02-2006
l2u
random function
Hello..

I want to have a random function so I could pass the next values in that way:

random(1,7) -> would return a random number in range 1-7 (i used boost for this)
random('A', 'Z') -> would return random letter in range A-Z (ABCDEF..)

but I have no idea what would be the right approach to return random letter in some range?

Would it be hard to do something like:
random(1, 'Z') -> random number or capital letter

• 11-02-2006
maxorator
A letter is just a one-byte integer.
If this works:
Code:

`random(1,7);`
then this should also work:
Code:

`random('A','Z');`
You don't need to make any special things for that.
• 11-02-2006
Prelude
>You don't need to make any special things for that.
You do if you want your code to be portable.

>but I have no idea what would be the right approach to return random letter in some range?
Create an array with your range and use a random index into that array:
Code:

```// Random number [1, 7) int r1 = random ( 1, 7 ); // Random letter const string letters ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); char r2 = letters[random ( 0, letters.size() )];```
>Would it be hard to do something like:
>random(1, 'Z') -> random number or capital letter
Not really, but it would depend on the exact behavior you want.
• 11-02-2006
SKeane
As Prelude says, don't assume that the codes assigned to letters are in a contiguous sequence if you want your code to be portable.
• 11-02-2006
maxorator
Code:

`random('A','Z');`
equals to
Code:

`random(65,90);`
as
Code:

`RegisterHotKey(hwnd,1,MOD_ALT,65);`
equals to
Code:

`RegisterHotKey(hwnd,1,MOD_ALT,'A');`
What do you mean by contiguous sequence?
• 11-02-2006
Daved
That is only true for the ASCII character set. There are other character sets out there.
• 11-02-2006
Prelude
>What do you mean by contiguous sequence?
C++ guarantees that the characters '0,'1','2','3','4','5','6','7','8','9' are in that order, with nothing inbetween. No such guarantee exists for any other characters. In other words, somewhere between 'A' and 'Z' could be something not even remotely resembling a letter.
• 11-02-2006
l2u
Code:

```int r1 = random ( 1, 7 ); // Random letter const string letters ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); char r2 = letters[random ( 0, letters.size() )];```
Shouldnt there be letters.size() - 1?
• 11-02-2006
Prelude
>Shouldnt there be letters.size() - 1?
It depends on how random is defined. I was assuming that it's the usual half open range that we all know and love. In that case, the random number returned would be never go below 0 and never go above 25, which is a perfectly valid index.
• 11-02-2006
l2u
I have a problems with my rand function... It will return the same value each time I call it with my program.. What am I doing wrong?

btw, I use boost.

Code:

```using namespace boost; int my_rand(int min, int max) {         mt19937 rng;         rng.seed( static_cast<unsigned> (time(0)) );         uniform_int<> dist(min, max);         variate_generator<mt19937&, uniform_int<> > die(rng, dist);         return die(); }```
• 11-02-2006
Prelude
>What am I doing wrong?
You only need to seed once. Or if the program is long running, very rarely.
• 11-02-2006
l2u
So I have to make mt19937 global and seed from the main() function?
• 11-03-2006
SKeane
What is this supposed to be doing?

Code:

`static_cast<unsigned>`
Just as a matter of interest.
• 11-03-2006
vart
Quote:

Originally Posted by l2u
So I have to make mt19937 global and seed from the main() function?

maybe enough to make it static inside the current function and initialize on the first call?
• 11-03-2006
Prelude
Quote:

Originally Posted by SKeane
What is this supposed to be doing?

Code:

`static_cast<unsigned>`

It's a C++ style cast. The equivalent C-style cast would be:
Code:

`rng.seed ( (unsigned)time ( 0 ) );`
What it's supposed to be doing is converting the time_t value that time returns to the unsigned value that seed expects. What it's actually doing may not be what it's supposed to be doing because that conversion isn't guaranteed to work.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last