Generating random numbers and Retreaving them in other forms in C#

I admit it, this probably has been answered half a google times before in other threads, but I have not been able to find exactly what I'm looking for.

I'm trying to make a rpg (hence the need for a number generator). But, I need to be able to have the spread of the nembers to be between 1 and 20, for example. Then, I need to call these numbers latter on. Obviously I'm learning the language and trying to modernize myself (I've only worked with QBASIC beore!) Can anyone help

Oh yeah, I should mention that a psudorandom number would suffice.




Answer this question

Generating random numbers and Retreaving them in other forms in C#

  • KompjoeFriek

    I didn't read the thread to a T, but the stuff there looks inefficient.  James Curran's shuffle technique described above is the standard way of generating permutations of random numbers: you start with an array of consecutive numbers, and swap each item successively with another randomly chosen element from the whole list. The resulting array is the permutation, generated without bias (other than the quality inherit in System.Random), done in exactly O(n) time (both big omega and big theta, to be pedantic), and done with no additional overhead.

    Actually the way I stated it is a slight variation of what was described, but as long as any element is in the certain slot with equal probability, it should be fine.  (I should prove that this is unbiased to be certain, but not now.)


  • Dick Campbell

    Use the Object Browser and go to System.Random. It's pretty self-explanatory. If you want an integer from 1 to 20 inclusive, you could do System.Random.Next(1,21); (I know, strange bounds.) If you want double, there's NextDouble.

    In general the Object Browser is a nifty way to find out what's out there, to fill in "what you don't know about the .NET framework."


  • GuyWithDogs

    I have made something like that. here is the code I used to make a dice roller for a D&D game.

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Threading;

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    private int RandomNumber(int min, int max)

    {

    Thread.Sleep(14);

    Random random = new Random();

    return random.Next(min, max);

    }

    private int RollSixSideDice(int min, int max)

    {

    Thread.Sleep(4);

    Random rolldice = new Random();

    return rolldice.Next(min, max);

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

    // Roll d6 for STR1 then set sleep timer

    int returnValueSTR1 = RollSixSideDice(1, 7);

    Thread.Sleep(returnValueSTR1);

    // Roll d6 for STR2 then set sleep timer

    int returnValueSTR2 = RollSixSideDice(1, 7);

    Thread.Sleep(returnValueSTR2);

    // Roll d6 for STR3 then set sleep timer

    int returnValueSTR3 = RollSixSideDice(1, 7);

    Thread.Sleep(returnValueSTR3);

    // Roll d6 for STR4 then set sleep timer

    int returnValueSTR4 = RollSixSideDice(1, 7);

    Thread.Sleep(returnValueSTR3);

    // Set an array to hold all numbers fro STR

    int[] ScoresSTR = new int[4];

    ScoresSTR[0] = returnValueSTR1;

    ScoresSTR[1] = returnValueSTR2;

    ScoresSTR[2] = returnValueSTR3;

    ScoresSTR[3] = returnValueSTR4;

    // sort the array to put the lowest value in [0]

    Array.Sort(ScoresSTR);

    STRroll1.Text = ScoresSTR[1].ToString();

    STRroll2.Text = ScoresSTR[2].ToString();

    STRroll3.Text = ScoresSTR[3].ToString();

    STRDisRoll.Text = ScoresSTR[0].ToString();

    int returnValueSTRt = ScoresSTR[1] + ScoresSTR[2] + ScoresSTR[3];

    tbSTR.Text = returnValueSTRt.ToString();

    Repet for each ability

  • GS80

    You need to clarify your request a bit more.

    Do you need

    a) a unlimited series of numbers which happen to be between 1 and 20 (with the potentially some repeats) or
    b) a series of 20 numbers which happen to be the number 1 to 20 in random order.

    Brian's method will give you (A). I suspect that you really want (B). But, don't worry -- almost as easy:

    The process is known as "Shuffling" (some call it "unsorting", but shuffling is a better name).

    • Fill an array with the numbers from 1 to 20 (in order)
    • Pick a random number from 1-19 (call it N). (using the method explained by Brian)
    • Swap the number at position N with the number at position 20.
    • Pick a random number from 1 -18
    • Swap the number at that position with the number at postion 19.
    • Repeat until you are picking a number between 1 & 2 and swapping with position 3.



  • CESAR DE LA TORRE

    I think he's rolling an isosahedron (20-sided) die, often used in role playing games such as D&D.
  • Menachem_P

    Moving to C# forum.
  • dwrayment

    -Using the System.Random.Next(1,21) method-

    Poor choice of wording there. System.Random is a class, and Next is a method of it, so what you need is:

    using System;
    // :
    // :
    Random rnd = new Random();
    int Str = rnd.Next(1,21);

    Note, once you've created the Random object, you can keep using it:

    Random rnd = new Random();
    int Str = rnd.Next(1,21);
    int Num2 = rnd.Next(1,100);
    int Num3 = rnd.Next(1,3); // etc.



  • Cla82

    -Using the System.Random.Next(1,21) method-

    I need to generate a random number as variable Strength (Str) from 1 to 20 on the click of a button (button7) as well as 7 other stats (I only have been working on getting one variable done before doing the rest). The problems that I encounter is that I just can't seem to figure out how to implement the examples given above into something that works from a button. Heck, I can't even figure out how to get the random number generated to be assigned as variable Str.

    Here's what I have:


    System.Random.Next(1, 21);

    int Str = Random;

    /*
    the second line is just a best guess as to how to assign the

    Apparently, I get two error messages reading:

    Error 1 An object reference is required for the nonstatic field, method, or property 'System.Random.Next(int, int)'
    Error 2 'System.Random' is a 'type' but is used like a 'variable'

    */



  • barryt.net

    Thank you guys. I'll need both methods throught the game. I'll go and experiment around to see if I can get it working.

  • ProSlamBanO

    There is a really good discussion of the second case in the VB express forum. Be sure to see the last post.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=395551&SiteID=1



  • Generating random numbers and Retreaving them in other forms in C#