Sign in to follow this  

rand function

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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>
[STAThread]
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");
Console.ReadLine();
}
}
}

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

}
}

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this