Random Number Generation with a Twist


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Random Number Generation with a Twist

  1. #1
    Join Date
    Feb 2004
    Posts
    111

    Random Number Generation with a Twist

    Ok. This is not going to be the simplest thing to describe in a post. So, if I've confused anyone, please let me know. What I want to do is done in three steps.

    Step One: I want to generate a random number to fall within a range between 0 and 100. I honestly think that this will be really easy. Now, this number will used to determine my game's start up options.

    Step Two: I want to test that number on a sliding scale of 0 to 100. For example, when the program first launches all of the start up options will have an equal chance of being selected. So the initial scale will be:

    0-20: Option 1
    21-40: Option 2
    41-60: Option 3
    61-80: Option 4
    81-100: Option 5

    Step Three: Based on the initial option selection the above scale will change to something like the below (where option 1 was selected the first time out)

    0-5: Option 1
    6-10: Option 2
    11-40: Option 3
    41-50: Option 4
    51-100: Option 5

    What I'm hoping to get from people are thoughts/input on how this might be done.

    Anyone? Any ideas?

    L -

  2. #2
    Join Date
    Feb 2004
    Posts
    111
    Here is how I'm generating the random number. Just in case people need to do the same thing.


    // Create a byte array and the generator to hold the random value.
    byte[] randomNumber = new byte[1];
    RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();

    // Fill the array with a random value.
    Gen.GetBytes(randomNumber);

    // Convert the byte to an integer value to make the modulus operation easier.
    int rand = Convert.ToInt32(randomNumber[0]);
    return rand % 100 + 1;

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Why not simply use the System.Random class?
    Code:
    int num = new System.Random().Next(101);
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Random Number Selection For Game
    By gordon87 in forum C++
    Replies: 2
    Last Post: 12-07-2007, 06:17 PM
  2. Replies: 18
    Last Post: 12-13-2006, 03:49 PM
  3. Help with Random number and text Box
    By cmvw in forum VB Classic
    Replies: 7
    Last Post: 06-28-2006, 11:04 AM
  4. random number generation, weight, etc.
    By rewclaus in forum VB Classic
    Replies: 0
    Last Post: 02-06-2006, 08:45 AM
  5. better random number generation
    By Jack Albert in forum VB Classic
    Replies: 2
    Last Post: 01-24-2001, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center