# rand function

This topic is 5485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, Prolly a very easy question to awnser for u guys. My c# documentation on the rand() and srand() functions isn't very clear about the range of the numbers generated. I've been using my own random number generator, but it doesn't take random seeds. all it does is take a number and with some formula i found on the internet i get a random number between -1 and 1. Please explain how rand works or give an example of a proper random number generator (with seeds). thanks, Marty... PS: I thought of using the last random number as an input for the next, so it will be generating random on a seed (that first number) but in the procedural textures i'm trying to make this gives very repeating results.

##### Share on other sites
C#? Shouldn't you be using Random? rand/srand is C. Either way the max value for rand is RAND_MAX defined in stdlib.h.

##### Share on other sites
Well, while I am not sure as to C# random functions, I will quickly explain the rand() and srand() as far as I know it.

srand() is used to "seed" the random generator. This is useful if you want the same results each time a certain number is used. This is used in generating "random" worlds that stay the same as you travel between them, but that is another concept all together. The way I seed the generator, in C/C++, is srand(GetTickCount()), which ensures randomness.

The function rand() itself returns a large "random" number. Since most uses of randomness want small numbers, like a guessing game, you would use the following syntax: rand()%number That % is the module(returns the remainder), which means you will get a number between 0 and that number, but never that number or above. Do not confuse the % as special syntax. This happened to me. I had only seen the function used as rand()%10 and thought it was all really strange syntax(I was just starting programming [headshake]).

This is how rand() and srand() work in C/C++, which might help you understand it in C#.

##### Share on other sites
The below code is something I am working on for a dice class, If you look at the section where it says
Console.WriteLine(mRand.Next(1,6).ToString()); This runs the random number generator with a min value of 1 and a max of 6

Example Code:

using System;

namespace DiceRoller
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
// Display Title
Console.WriteLine("Basic Dice Roller, Test for Dice Classes");

RandomNumberGen mRand = new RandomNumberGen;

Random mRand = new Random(unchecked((int)DateTime.Now.Ticks));

for(int i=1; i<=10; i++)
{
Console.WriteLine(mRand.Next(1,6).ToString());
}

// Pause Screen to Verify program
Console.WriteLine("\n\rPress Enter to Exit");
}
}
}

using System;
using System.Security.Cryptography; // Needed for a descent random number generator.

namespace DiceRoller
{
/// <summary>
/// Summary description for RandomNumberGen.
/// This random generator uses the crypto service provider
/// because the normal random functions in .NET
/// have repeating number strings and other such problems.
/// </summary>
public class RandomNumberGen
{
public RandomNumberGen()
{
//
// TODO: Add constructor logic here
//
}
byte[] oRandByte = new byte[0];

RNGCryptoServiceProvider oRng = new RNGCryptoServiceProvider();

public int GetRandomNumber(int min, int max)
{
oRng.GetNonZeroBytes(oRandByte);
Random oRand = new Random(Convert.ToInt32(oRandByte));
return oRand.Next(min, max);
}

}
}

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22