C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2009, 12:28 PM   #1
Registered User
 
Join Date: Jan 2007
Posts: 102
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
Bargi is offline   Reply With Quote
Old 03-11-2009, 12:47 PM   #2
Registered User
 
Join Date: Oct 2008
Posts: 98
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.

Quote:
Also, I have to put the logic whether it from unit place or tens place.
I don't quite understand what this means however.
Sparrowhawk is offline   Reply With Quote
Old 03-11-2009, 12:48 PM   #3
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,203
Quote:
Originally Posted by Bargi View Post
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!

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

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 03-11-2009, 12:52 PM   #4
Registered User
 
Join Date: Oct 2008
Posts: 98
Quote:
Originally Posted by MK27 View Post
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!

[edit] 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!

[edit] - 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!

Last edited by Sparrowhawk; 03-11-2009 at 12:54 PM.
Sparrowhawk is offline   Reply With Quote
Old 03-11-2009, 01:03 PM   #5
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,203
Quote:
Originally Posted by Sparrowhawk View Post
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 03-11-2009, 01:16 PM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
Quote:
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.

Quote:
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.

Quote:
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 03-11-2009, 01:16 PM   #7
Registered User
 
Join Date: Jan 2007
Posts: 102
Thanks For help ......
Its really solve the problem
Bargi is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
rapid random number generation problem Newton C Programming 17 09-19-2008 02:08 PM
Random number in range generation. hebali C Programming 19 03-04-2008 10:46 AM
adding a number to a number bigmac(rexdale) C Programming 11 10-24-2007 12:56 PM
random number tutorial 7stud C++ Programming 3 07-26-2005 02:41 PM
Random Number Generator Ikurik C++ Programming 16 08-17-2003 07:34 PM


All times are GMT -6. The time now is 01:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22