Thread: Percent of each random number

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    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
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  2. #2
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    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;

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    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
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    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.

    So as far as C is concerned your talking about this type of setup:

    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;
    }
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That's one way to implement it, yes.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    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
    So as far as C is concerned your talking about this type of setup:

    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;
    }

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    hmmm... the second bit of code seems easyer to read.. why is that?

  10. #10
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Its structured better.
    Microsoft is merely an illusion, albeit a very persistant one.

  11. #11
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Quote Originally Posted by adrianxw
    >>> 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 Originally Posted by adrianxw
    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 Originally Posted by adrianxw
    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.
    Well, that's what comments are for .
    Quote Originally Posted by adrianxw
    Keep it simple.
    Couldn't agee more. And as you can see, my example is clearely more simple that the first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM