![]() |
| | #1 |
| Registered User Join Date: Jan 2007
Posts: 102
| Help regarding random number 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();
}
|
| Bargi is offline | |
| | #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();
}
Quote:
| |
| Sparrowhawk is offline | |
| | #3 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,203
| Quote:
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; Good luck! [edit] actually you can kiss that award goodbye, Sparrowhawk just outdid you by a mile...DO NOT use the Sparrowhawk method. Honestly. | |
| MK27 is offline | |
| | #4 | |
| Registered User Join Date: Oct 2008
Posts: 98
| Quote:
![]() 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 | |
| | #5 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,203
| Quote:
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. | |
| MK27 is offline | |
| | #6 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,339
| Quote:
Quote:
Code: do
{
i = rand();
}
while (i >= 40);
Quote:
Code: int random = rand() / (RAND_MAX / 40 + 1);
__________________ 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 | |
| | #7 |
| Registered User Join Date: Jan 2007
Posts: 102
| Thanks For help ...... Its really solve the problem |
| Bargi is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |