It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
So I have this
Random random1 = new Random();
int intrandom1 = random1.Next();
I want to put a long after the .Next. How do I do that? It only accepts ints.
First idea: A long as 64 bit integer, is the combination of 2 32 bit integers, so you can use:
((long)random1.Next() << 32) | random1.Next()
Or perhaps
((long)random1.Next() <<< 32) | random1.Next()
if you use java (?) and need a unsigned shift
edit: does not look like Java. Java has random1.nextLong() for that. Perhaps C#? I do not know that
It can be generated arbitrary long number by using a simple linked list. Just imagine that every node from list can store a number random generated, and a function can be read that list like an unitary number. Using an algorithm like that you can obtain an arbitrary long random number.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Function receives char[,].
For example if it it takes
000
LAD
0B0
Traversing should print out all possible combinations of non-zero chars:
L
LA
LAD
LAB
A
AL
AB
AD
and so on
private void Traverse(char[,] area)
{
}
The easiest way is to write a recursive function with two strings, initial and output. I assume you want combinations, not permutation so it's a little easier. The base case is checking if initial is empty, then print out the output. The step is removing a character from initial and calling the recursive function twice, one with the unchanged output and one with the removed character added into the output. If the removed character is 0, however, then you only call the function once (removing the 0 without adding anything to the output.)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone do this for me please.
Write a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or ommitted.
Here is the hint to solve the above question: use Random rnd=new Random() to create a new instance of the random number generator and rnd.Next(1,1000) to get a random integer value between 1 and 1000.
Many Thanks.
I suspect your examiner is probably looking for a Fisher-Yates Shuffle, so the following answer is probably too concise.
Random r = new Random();
public int[] GetArr()
{
return Enumerable.Range(1, 1000).OrderBy(_ => r.Next()).ToArray();
}
did you try :
-generate a random number
-check if he is allready in the tab
-put it in your array,in first free position
the only problem will be that it will drastycally slow down on the end
You might want to try something where you create a class which contains an int and a bool, the bool can be "isused", then the int can be the index, then use a random number generator, get the number associated with the output, then check if that number is in use by an array of your class, if not then push it onto a list of ints, then once completed return the list as an array?
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
In my one application i want to shuffle string contents and in another application if i pass this shuffled string as input then that application has to return original string value.Is there any method is available to do this in dotnet plateform?
you can use Fisher–Yates shuffle algorithm.
it is an algorithm for generating a random permutation of a finite
set—in plain terms, for randomly shuffling the set.
The basic method given for generating a random permutation of the numbers 1–N goes as follows:
Write down the numbers from 1 to N.
Pick a random number k between one and the number of unstruck
numbers remaining (inclusive).
Counting from the low end, strike out the kth number not yet struck
out, and write it down elsewhere.
Repeat from step 2 until all the numbers have been struck out.
The sequence of numbers written down in step 3 is now a random
permutation of the original numbers.
there is an example : shuffle algorithm
and good one here
Definitely not out of the box, but create yourself a struct
struct ShuffleChar{
char c;
int index;
}
and when you shuffle your letters, assign each letter an index so you can put them together again. Remember, a string only makes sense if the letters are in the correct order, changing that order will destroy that information and with high probability you cannot restore it...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
It has soon been 12h of trying this and I can't get it to work. I have read all the threads I can find and nothing helps. I have tried to fiddle around with Excel Wrapper as well but it doesn't work. I'm new to C# and I'm trying to do a bullshit generator. What I am trying to do is reading a bunch of words from A1-A5, B1-B5 and C1-C5 in an .xlsx-file and putting them together in a textbox (I'm using Visual Studio) when clicking a button.
If anyone reads this and could give me a hint it would be much appreciated. Thanks in advance.
First you should pull the info from the xls doc to a collection (array, list, etc...)
The code for this should be easy to find online.
You are also going to need a random number generator:
Random rnd = new Random();
Then you are going to have the button click event select 2 random numbers, one for row and one for column, from your collection (2D array in this case):
int row_max = stuff[][].GetLength(0);
int col_max = stuff[][].GetLength(1);
int row = rnd.Next(0, row_max-1)
int col = rnd.Next(0, col_max-1); //between 0 and the number of columns
textbox1.text = textbox1.text + stuff[row][col].ToString();
This is indicative only, but all the parts of this can be easily googled.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list and a column; the column might have many values, and each value's length may vary.
If the length exceeds 100, I want to append /n at the end of it.
Need your help.
I give it a try, though a lot of questions are open (what type of list, what a column, ...).
In short: you can use String.Insert to insert text at a specified postition.
Assuming you have a List<Foo>, the class Foo has a string property Value (your column). If it's Length exceeds 100 the line should be wrapped:
foreach(Foo foo in foos)
{
if(foo.Value.Length > 100)
foo.Value = foo.Value.Insert(100, Environment.NewLine);
}
http://msdn.microsoft.com/en-us/library/system.string.insert.aspx
Here's running code with sample data.
http://ideone.com/sUKLk