# Thread: Help regarding random number

1. ## Help regarding random number

Hi ,

I am working in a problem in which I have to print elements in array using random number logic.

The logic is that the elements of character should be printed randomly from the array. I have used random function for this.

The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.

But the problem is that the array size is 0nly 40 and the number I get from generator some times greater than 40,so in this case repeated iterations will be there,which is not good practice.Also, I have to put the logic whether it from unit place or tens place.

Can anybody help me in implementing this.Actually I want to put the optimized solution.

The code is as follow:
Code:
```#include<stdio.h>
#include<conio.h>
#include<time.h>

int main()
{
char alpha[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int count =0,i,mul=0;
int seed = (int)time(NULL);
clrscr();
srand(seed);

i = rand();
printf("%d",i);

while(count!=2)
{
mul*=10;
mul+=i%10;
i = i/10;
count++;
}

printf("\n Value=%c",alpha[mul]);

getch();
}```
Thanks

2. Hmm I'm not sure if I understand what it needs to do exactly...

If you just want the random number to be equal to or below 40 everytime... Put in a loop to keep randing until you get a suitable number (a number less than 40)

Code:
```while(i%100>=40)
{
i=rand();
}```
I'm not sure if you meant you didn't want to do that with the "repeated iterations" comment but it's fine in terms of programming practice to do it in that fashion.

Also, I have to put the logic whether it from unit place or tens place.
I don't quite understand what this means however.

3. Originally Posted by Bargi
The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.
That is so crazy, you may get my first ever "craziest method of the month" award.

rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
Code:
```int divisor=RAND_MAX/40;
int random=rand()/divisor;```
I can't remember if rand() starts at 0 or 1, you may have to find some documentation and read it yourself.

Good luck!

 actually you can kiss that award goodbye, Sparrowhawk just outdid you by a mile...DO NOT use the Sparrowhawk method. Honestly.

4. Originally Posted by MK27
That is so crazy, you may get my first ever "craziest method of the month" award.

rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
Code:
```int divisor=RAND_MAX/40;
int random=rand()/divisor;```
I can't remember if rand() starts at 0 or 1, you may have to find some documentation and read it yourself.

Good luck!

 actually you can kiss that award goodbye, Sparrowhawk just outdid you by a mile...DO NOT use the Sparrowhawk method. Honestly.
I just saw this, very interesting... Yes you're right, your method is much better. Can I find a list of system specified values like that one somewhere? I'm definitely interesting in knowing all of them now

Thanks!

 - To be fair though, it was laziness on my part to read the entire article in the C++ reference... It identifies RAND_MAX and I didn't see that at all... Nevertheless, still interested in finding a listing of these things somewhere!

5. Originally Posted by Sparrowhawk
Can I find a list of system specified values like that one somewhere? I'm definitely interesting in knowing all of them now

Thanks!
Absolutely Not. The general protocol with C documentation is to exclude irrelevant details such as this, so that you can spend the afternoon wondering if you are really the dumbest ass in the universe. Then, after an undefined period of having not given up anyway, all those wasted, fretful hours will count as some form of experience with C documentation, so that the first thing you ask yourself when trying to learn something new is: "What aren't they telling me now?"

I saw an API reference last week that started "You know I wrote this and lots of people use it, so it must work -- but how? By the time you are done here, you might as well have written the whole thing, from scratch, yourself..." I swear it said that.

6. Originally Posted by Bargi
The logic I implementing is that, I took the random number using function rand() and pick up the last two digit from it and with respect to them , I took the value from char array and print it down.
You have a right idea, but a wrong implementation. If you really want to do things that way, then just writing rand() % 40 will do. The minor disadvantage is that the distribution would no longer be theoretically uniform.

Originally Posted by Sparrowhawk
If you just want the random number to be equal to or below 40 everytime... Put in a loop to keep randing until you get a suitable number (a number less than 40)
This is also a right idea, but a wrong implementation, since you should then just write:
Code:
```do
{
i = rand();
}
while (i >= 40);```
However, [0, 40) is a rather narrow range for this approach. A variant of this approach that works with narrow ranges is to divide [0, RAND_MAX] into 40 equally sized "buckets", with left over numbers left out. The advantage in both cases is that the distribution remains uniform in theory, but then someone once pointed out to me that it is probably overkill anyway.

Originally Posted by MK27
rand() delivers a random number up to the value of RAND_MAX, which is system specific. So, if all you want is a number from 1-6, you divide RAND_MAX by 6, and use that as a divisor on the return value of rand():
This should work, but probably should be changed to:
Code:
`int random = rand() / (RAND_MAX / 40 + 1);`
with the +1 to accomodate the [0, 40) rather than [0, 40] range. I think it has the same minor disadvantage as with the modulo method.

7. Thanks For help ......
Its really solve the problem

Popular pages Recent additions