本文整理汇总了C#中Random.Next方法的典型用法代码示例。如果您正苦于以下问题:C# Random.Next方法的具体用法?C# Random.Next怎么用?C# Random.Next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Random
的用法示例。
在下文中一共展示了Random.Next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
/* Write a program that finds in given array of integers a sequence of given sum S (if present).
* Example: {4, 3, 1, 4, 2, 5, 8}, S=11 {4, 2, 5} */
Console.Write("Enter array lenght (or 0 for array autogeneration): ");
int n = int.Parse(Console.ReadLine());
int[] arr;
if (n == 0)
{
Random rnd = new Random();
n = rnd.Next(10, 20);
arr = new int[n];
Console.WriteLine("Generated N is: {0}", n);
Console.WriteLine("Generated array is:");
for (int i = 0; i < n; i++)
{
arr[i] = rnd.Next(18) - 9;
Console.Write("{0,3}", arr[i]);
}
Console.WriteLine();
}
else
{
arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write("Element {0}: ", i + 1);
arr[i] = int.Parse(Console.ReadLine());
}
}
Console.Write("Enter sum to search for: ");
int sum = int.Parse(Console.ReadLine());
Console.WriteLine();
// input is done
bool noSolution = true;
int allSum = 0;
for (int i = 0; i < n; i++) // start form first element to end of array
{
allSum = allSum + arr[i]; // sum of a[0]+a[1]+...+a[i];
int currSum = allSum;
for (int j = 0; j <= i; j++) // start from first element to i-th element
{
if (currSum == sum) // is currnet sum = sum?
{
Console.Write("Sum = {0} ->", sum);
for (int k = j; k <= i; k++) //yes - print sequence a[j]...a[i]
Console.Write("{0,3}", arr[k]);
Console.WriteLine();
noSolution = false;
}
currSum = currSum - arr[j]; // subtract a[j] from sum a[j]...a[i];
}
}
if(noSolution)
Console.WriteLine("No sequence with sum of {0}.",sum);
}
示例2: reset
public void reset()
{
Random random = new Random();
int localtag = 0;
localtag++;
// TO TRIGGER THE BUG:
// remove the itself from parent from an action
// The menu will be removed, but the instance will be alive
// and then a new node will be allocated occupying the memory.
// => CRASH BOOM BANG
CCNode node = getChildByTag(localtag - 1);
CCLog.Log("Menu: %p", node);
removeChild(node, false);
// [self removeChildByTag:localtag-1 cleanup:NO];
CCMenuItem item1 = CCMenuItemFont.itemFromString("One", this, menuCallback);
CCLog.Log("MenuItemFont: %p", item1);
CCMenuItem item2 = CCMenuItemFont.itemFromString("Two", this, menuCallback);
CCMenu menu = CCMenu.menuWithItems(item1, item2);
menu.alignItemsVertically();
float x = random.Next() * 50;
float y = random.Next() * 50;
menu.position = CCPointExtension.ccpAdd(menu.position, new CCPoint(x, y));
addChild(menu, 0, localtag);
//[self check:self];
}
示例3: Main
public static void Main()
{
Stopwatch watch = new Stopwatch();
Random rand = new Random();
watch.Start();
for (int i = 0; i < iterations; i++)
DayOfYear1(rand.Next(1, 13), rand.Next(1, 29));
watch.Stop();
Console.WriteLine("Local array: " + watch.Elapsed);
watch.Reset();
watch.Start();
for (int i = 0; i < iterations; i++)
DayOfYear2(rand.Next(1, 13), rand.Next(1, 29));
watch.Stop();
Console.WriteLine("Static array: " + watch.Elapsed);
// trying to modify static int []
daysCumulativeDays[0] = 18;
foreach (int days in daysCumulativeDays)
{
Console.Write("{0}, ", days);
}
Console.WriteLine("");
// MY_STR_CONST = "NOT CONST";
}
示例4: GetCombinations
public void GetCombinations()
{
//given a input
//
Random rnd = new Random();
int n = rnd.Next(2, 10);
int k = rnd.Next(n + 1); // RND boundaries are exclusive so n+1 will allow inclusive boundaries
// Get combinations
//
var combination = generator.GetCombinations((uint)n, (uint)k);
// Get Control value
//
var controlValue = GetControlCombinations(n, k);
//do validate if output matches expected value
//
Assert.AreEqual<uint>(
(uint)(controlValue),
combination,
"C({0},{1}) was computed as {2} instead of expected value of {3}",
n,
k,
combination,
controlValue);
}
示例5: RandomStringArray
static string[] RandomStringArray()
{
Random rand = new Random();
int len = rand.Next(3, 10); // [3;10)
string[] stringArr = new string[len];
// let's build the words observing the frequency of the letters in English
// so for example the vowels will appear more often
// http://www.oxforddictionaries.com/words/what-is-the-frequency-of-the-letters-of-the-alphabet-in-english
double[] frequencies = { 0.002, 0.004, 0.007, 0.010, 0.020, 0.031, 0.044, 0.061, 0.079, 0.100, 0.125, 0.155, 0.185, 0.217, 0.251, 0.287, 0.332, 0.387, 0.444, 0.511, 0.581, 0.652, 0.728, 0.803, 0.888, 1.000, };
char[] letters = { 'q', 'j', 'z', 'x', 'v', 'k', 'w', 'y', 'f', 'b', 'g', 'h', 'm', 'p', 'd', 'u', 'c', 'l', 's', 'n', 't', 'o', 'i', 'r', 'a', 'e', };
for (int i = 0; i < len; i++)
{
int wordLen = rand.Next(1, 10); // determines the length of the current string
string word = string.Empty;
for (int j = 0; j < wordLen; j++)
{
double frequency = rand.Next(2, 1000) / 1000d; // [0.002;0.999]
int index = Array.BinarySearch(frequencies, frequency);
if (index < 0) index = ~index;
char letter = letters[index];
word += letter;
}
stringArr[i] = word;
}
return stringArr;
}
示例6: Main
private static void Main()
{
// Deklarations
int result = -1;
Random rand = new Random();
int lenghtOfArray = rand.Next(10, 20);
int[] arrayOfInteger = new int[lenghtOfArray]; // Enter array here and comment down (line 42)
//arrayOfInteger = { 13, 14, 15, 16, 0, 1, 2, 3, 17, 18, 19, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
//arrayOfInteger = {10, 8, -7, -10, -6, 1};
//{ 2, 3, -6, -1, 2, -1, 6, 4, -8, 8 };
//{4, 3, 1, 4, 2, 5, 8}{ -4, 6, -7, 8, 6, -10, -3, 0 };
//{ 2, 1, 2, 4, 3, 5, 2, 6 };9 0 6 -10 4 2 10
for (int i = 0; i < arrayOfInteger.Length; i++)
{
arrayOfInteger[i] = rand.Next(-5, 5); // comment if enter hardcore array
Console.Write(arrayOfInteger[i] + " ");
}
Console.WriteLine();
// Solution
result = CheckNeighbors(arrayOfInteger);
if (result == -1)
{
Console.WriteLine("No bigger than its neighbors");
}
else
{
Console.WriteLine("{0} is bigger than its neighbors; index: {1}", arrayOfInteger[result], result);
}
}
示例7: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
int x, y;
string strValidation = null;
rnd = new Random();
Response.ContentType = "image/jpeg";
Response.Clear();
Response.BufferOutput = true;
strValidation = GenerateString();
Session["strValidation"] = strValidation;
Font font = new Font("Arial", (float)rnd.Next(17, 20));
Bitmap bitmap = new Bitmap(200, 50);
Graphics gr = Graphics.FromImage(bitmap);
gr.FillRectangle(Brushes.LightGreen, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
gr.DrawString(strValidation, font, Brushes.Black, (float)rnd.Next(70), (float)rnd.Next(20));
// gr.DrawLine(new Pen(Color.White), new Point(0, rnd.Next(50)), new Point(200, rnd.Next(50)));
// gr.DrawLine(new Pen(Color.White), new Point(0, rnd.Next(50)), new Point(200, rnd.Next(50)));
// gr.DrawLine(new Pen(Color.White), new Point(0, rnd.Next(50)), new Point(200, rnd.Next(50)));
for (x = 0; x < bitmap.Width; x++)
for (y = 0; y < bitmap.Height; y++)
if (rnd.Next(4) == 1)
bitmap.SetPixel(x, y, Color.LightGreen);
font.Dispose();
gr.Dispose();
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
bitmap.Dispose();
}
示例8: MakeRandom
private XLoper MakeRandom(Random r)
{
XLoper x = new XLoper();
x.Type = XLoper.xlTypeNil;
int choice = r.Next(7);
switch (choice)
{
case 0:
x.Type = XLoper.xlTypeStr;
x.Str = MakeRandomString(r.Next(1000));
break;
case 1:
x.Type = XLoper.xlTypeNum;
x.Num = r.NextDouble() * 1000;
break;
case 2:
x.Type = XLoper.xlTypeInt;
x.W = r.Next(1000);
break;
case 3:
x.Type = XLoper.xlTypeBool;
x.Bool = r.Next(2) == 1;
break;
case 4:
x.Type = XLoper.xlTypeStr;
x.Str = "";
break;
}
return x;
}
示例9: RollDice
// You and me are gonna bing!
public void RollDice()
{
int roll;
Random rnd = new Random();
roll = rnd.Next(1, 6) + rnd.Next(1, 6);
FindRewards(roll);
}
示例10: Main
public static void Main(string[] args)
{
int T = 6;
int N = int.Parse(args[0]);
int Q = int.Parse(args[1]);
Random rand = new Random();
int MAX_VALUE = 32768 + 1;
Console.WriteLine(T);
for(; T > 0; T--){
Console.WriteLine("{0} {1}", N, Q);
for(int i = 0; i < N; i++){
if(i != 0) Console.Write(' ');
Console.Write(rand.Next(0, MAX_VALUE));
}
Console.WriteLine();
for(int i = 0; i < Q; i++){
int a = rand.Next(0, MAX_VALUE);
int p = rand.Next(1, N + 1);
int q = rand.Next(p, N + 1);
Console.WriteLine("{0} {1} {2}", a, p, q);
}
}
}
示例11: Main
// Write a method that adds two positive integer numbers
// represented as arrays of digits (each array element arr[i]
// contains a digit; the last digit is kept in arr[0]).
// Each of the numbers that will be added could have up to 10 000 digits.
static void Main()
{
//variables
int[] arrayOne = new int[6];
int[] arrayTwo = new int[10];
Random rand = new Random();
//fill the arrays with Random numbers
for (int i = 0; i < arrayOne.Length; i++)
{
arrayOne[i] = rand.Next(9);
}
for (int i = 0; i < arrayTwo.Length; i++)
{
arrayTwo[i] = rand.Next(9);
}
#region print arrays
Console.Write("{ ".PadLeft(10));
Console.Write(String.Join(",", arrayOne));
Console.WriteLine(" }");
Console.Write("{ ");
Console.Write(String.Join(",", arrayTwo));
Console.WriteLine(" }");
Console.WriteLine();
#endregion
#region print result
Console.Write("{ ");
Console.Write(String.Join(",", AddArrays(arrayOne, arrayTwo))); //call AddArrays method
Console.WriteLine(" }");
#endregion
}
示例12: Main
public static void Main(Client client)
{
string spellName = "adevo mas hur";
ushort spellMana = 180;
Random rand = new Random();
while (true)
{
Thread.Sleep(rand.Next(1000 * 10, 1000 * 15));
if (client.Player.Mana >= spellMana && client.Player.ManaPercent >= 80)
{
client.Player.MakeRune(spellName, spellMana);
Thread.Sleep(rand.Next(1000 * 2, 1000 * 5));
}
foreach (ushort id in client.ItemList.Food.All)
{
Item food = client.Inventory.GetItem(id);
if (food != null)
{
food.Use();
Thread.Sleep(rand.Next(1000 * 2, 1000 * 6));
break;
}
}
}
}
示例13: Main
static void Main()
{
Matrix first = new Matrix(5, 5);
Matrix second = new Matrix(5, 5);
Random random = new Random();
for (int rows = 0; rows < first.rows; rows++)
{
for (int columns = 0; columns < first.columns; columns++)
{
first[rows, columns] = random.Next(-150, 150);
second[rows, columns] = random.Next(-150, 150);
}
}
Console.WriteLine("Matrix 1:");
Console.WriteLine(first);
Console.WriteLine("Matrix 2:");
Console.WriteLine(second);
Console.WriteLine("Result after addition:");
Console.WriteLine(first + second);
Console.WriteLine("Result upon substraction:");
Console.WriteLine(first - second);
Console.WriteLine("Matrices multiplication:");
Console.WriteLine(first * second);
}
示例14: AddMonths_TestData
public static IEnumerable<object[]> AddMonths_TestData()
{
TaiwanCalendar calendar = new TaiwanCalendar();
Random random = new Random(-55);
DateTime randomDateTime = TaiwanCalendarUtilities.RandomDateTime();
if ((calendar.MaxSupportedDateTime.Year - randomDateTime.Year - 1911) > 1000)
{
yield return new object[] { randomDateTime, random.Next(1, 1000 * 12) };
}
else
{
yield return new object[] { randomDateTime, random.Next(1, (calendar.MaxSupportedDateTime.Year - randomDateTime.Year - 1911) * 12) };
}
if ((calendar.MinSupportedDateTime.Year - randomDateTime.Year) < -1000)
{
yield return new object[] { randomDateTime, random.Next(-1000 * 12, 0) };
}
else
{
yield return new object[] { randomDateTime, random.Next((calendar.MinSupportedDateTime.Year - randomDateTime.Year) * 12, 0) };
}
yield return new object[] { calendar.MaxSupportedDateTime, 0 };
yield return new object[] { calendar.MinSupportedDateTime, 0 };
}
示例15: AskForComputerMoveThree
//Method that asks the computerplayer for a move in 3x3 game
public int AskForComputerMoveThree(char[] board, char turn)
{
Random rnd = new Random();
int input;
// Ask for players move
Console.Write("Player " + turn + ". Make your move (0 - " + (BoardSize - 1) + "): ");
input = rnd.Next(0, 8);//Random number for where the computer wan´t to place it´s marker
// Make the move
while (true)
{
bool validMove = MakeMove(board, turn, input);
if (validMove == false)
{
Console.Write("Pick a new location (0 - " + (BoardSize - 1) + "): ");
//New random number to pick a new location if space is occupied or invalid
input = rnd.Next(0, 8);
}
else
break;
}
return input;
}