# Percent of each random number

• 11-09-2004
CrackerJack
Percent of each random number
What's happening guys?

Alright I had a real quick question. Is there a way you can munipulate the random function in VB so that you can specify how many times each number is picked?

Like say you wanted to have 1 picked from 1 to 3...55% of the time.

Any help would be appreciative.

CJ
• 11-09-2004
Xzyx987X
It's easy. I'll write it in C since I dunno VB.

Code:

```int randNum = rand() % 101; if(randNum <= 55) randNum = 1; else randNum = randNum / 22.5 + 1;```
• 11-09-2004
CrackerJack
Quote:

Originally Posted by Xzyx987X
It's easy. I'll write it in C since I dunno VB.

Code:

```int randNum = rand() % 101; if(randNum <= 55) randNum = 1; else randNum = randNum / 22.5 + 1;```

OK..so what your doing is using the random number w/ mod 101 (as percent I assume), and then using the if statement to force the outcome from that result? Well what if you want multiple percents? Like you want 55% - 1, 20% - 6, and 10% - 13?

And how did you get the line you did for the else statement?

CJ
• 11-09-2004
MrWizard
Let's say you have a list of perctanges and their corresponding values. Let's take your example:

55% - 1
20% - 6
10% - 13

I would just create a few ranges for your percents. For instance, 1 to 55 would be range 1 corresponding to the value 1. Range 2 would be 56 to 75, correspoding to value 6. Range 3 would be 76 to 85 corresponding to 13. Then you simply get a random number between 1 and 100 and determine which range it falls into and then look up the value for that range. I think this would be a simple solution to your problem.
• 11-09-2004
CrackerJack
Quote:

Originally Posted by MrWizard
Let's say you have a list of perctanges and their corresponding values. Let's take your example:

55% - 1
20% - 6
10% - 13

I would just create a few ranges for your percents. For instance, 1 to 55 would be range 1 corresponding to the value 1. Range 2 would be 56 to 75, correspoding to value 6. Range 3 would be 76 to 85 corresponding to 13. Then you simply get a random number between 1 and 100 and determine which range it falls into and then look up the value for that range. I think this would be a simple solution to your problem.

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 main () { int i, RandNum; srand((unsigned)time(NULL)); for (i = 0; i < 3; i++) {                 RandNum = (int) N * rand() / (RAND_MAX + 1);         if (RandNum <= 55 && RandNum >= 0)                 RandNum = 1;                 if (RandNum <= 85 && RandNum >= 56)                 RandNum = 2;                 if (RandNum <= 100 && RandNum >= 86)                 RandNum = 3;         printf("This was the number picked: %d \n", RandNum);         RandNum = 0; }                         return 0; }```
• 11-09-2004
MrWizard
That's one way to implement it, yes.
• 11-09-2004
Xzyx987X
Quote:

Originally Posted by CrackerJack
OK..so what your doing is using the random number w/ mod 101 (as percent I assume), and then using the if statement to force the outcome from that result? Well what if you want multiple percents? Like you want 55% - 1, 20% - 6, and 10% - 13?

And how did you get the line you did for the else statement?

CJ

Opps. Lol, I was in a hurry to write a response, nm that.

Quote:

Originally Posted by CrackerJack

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 main () { int i, RandNum; srand((unsigned)time(NULL)); for (i = 0; i < 3; i++) {                 RandNum = (int) N * rand() / (RAND_MAX + 1);         if (RandNum <= 55 && RandNum >= 0)                 RandNum = 1;                 if (RandNum <= 85 && RandNum >= 56)                 RandNum = 2;                 if (RandNum <= 100 && RandNum >= 86)                 RandNum = 3;         printf("This was the number picked: %d \n", RandNum);         RandNum = 0; }                         return 0; }```

This would be more efficient:

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 main () { int i, RandNum; srand((unsigned)time(NULL)); for (i = 0; i < 3; i++) {                 RandNum = (int) N * rand() / (RAND_MAX + 1);         if (RandNum <= 55)                 RandNum = 1;                 else if (RandNum <= 85)                 RandNum = 2;                 else                 RandNum = 3;         printf("This was the number picked: %d \n", RandNum);         RandNum = 0; }                         return 0; }```
• 11-10-2004
>>> This would be more efficient:

It may appear to be more efficient. A modern compiler would probably optimise both to the same executable code. Only real expert coders can optimise reliably better then an optimising compiler.

Rather, what happens is people try to optimise their code, making it less readable in the process, and end up with the same executable anyway.

Forget trying to squeeze "that kind" of efficiency out of your code, let the compiler do it. Code maintenance is a much higher cost activity then development since the maintainer may not be you, and even if it is, you probably won't remember what you did and why if you come back to it several yaers later.

Keep it simple.
• 11-10-2004
PanzTec
hmmm... the second bit of code seems easyer to read.. why is that?
• 11-10-2004
dagdarian
Its structured better.
• 11-10-2004
Xzyx987X
Quote:

>>> This would be more efficient:

It may appear to be more efficient. A modern compiler would probably optimise both to the same executable code. Only real expert coders can optimise reliably better then an optimising compiler.

Actually, the real best approach if you're looking for speed is just to write it in ASM. The efficient part in my example was that it was easier to read, and didn't take as long to type.
Quote:

Rather, what happens is people try to optimise their code, making it less readable in the process, and end up with the same executable anyway.

That depends, but I'm not going to argue about it since I wholeheartedly agree that unless there is some reason a particular peice of code had to run fast, it's useless to try to optimise it.
Quote: