Character Shuffling and Re-Arranging in c# [closed] - c#

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...

Related

Function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order [closed]

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?

Distribute integers for tag cloud weighting [closed]

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 of words that I want to display in a web page as a tag cloud. Each word has a corresponding 'weight' which determines how big the word should appear in the tag cloud.
Let's say that this array contains the number of times each word has been used to tag a document and I want to use these values as the weighting for each word:
int [] ints = new int[] { 1, 2, 4, 3, 2, 1, 4, 2, 1000};
I want the range of weightings to be within a specified range such that the rendering code has a predictable set of numbers to deal with however I don't want to simply normalize these integers because then all but the last will be essentially zero.
If this were to happen then there'd be a single item in the tag cloud that would be very big and all the other tags would be tiny. I'm looking for a way of squeezing all the of the integers into a limited range whilst preserving a degree of diversity.
So how can I transform the set such that the final large value doesn't make the others insignificant?
Use logarithms to pre-process the data and bring down the scale of the numbers. Choose the base to the logarithm based on the data values. For the purpose of a tag cloud, using log to base 2 would be most ideal in my opinion. Once you have found the log, normalize the resulting numbers.

Creating long randoms [closed]

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.

Array containing only 0 and 1 values [closed]

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 need an array which will only contain the values 0 and 1. Will bool[] be good enough for me? Or is there something lighter weight?
EDIT:
I dont have memory constraints but that array is made and passes online All The Time with big files passes concurrently with that array. I want the maximun optimization so the big files wont be delayed
A bool is probably not the best way to do it. Depends how many numbers you have got.
It is important to realise that even though a bool is a single bit, it requires a full byte in memory.
A BitArray on the other hand takes care of this for you and is more space efficient, although ever so slightly less time efficient.
http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx
Depends on your constraints, if it is not for a constrained environment a bool array will work just fine.
Bool Array is good enough. You can consider "false" as 0 and "true" as 1.
May be you need BitArray, the sequence of 1 and 0.
bool[] would do the trick... If your 0 & 1 numbers are in fact just "Flags" and not real numbers.
Using an Enum with the Flags attribute is another option. This would allow you to have an Intention Revealing Name for both boolean values.
[Flags()]
public enum TheFlags
{
NoneSet = 0,
FirstSet = 1,
SecondSet = 2
}
Then you can check if "First" is set like so:
TheFlags flags = TheFlags.FirstSet;
if (flags.HasFlag(TheFlags.FirstSet))
Console.WriteLine("First flag is set!");

How to generate 6 digit random number based on current date & time [closed]

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 11 years ago.
what logic i can use to generate 6 digit random number based on current date and time. suppose i want to generate a 6 digit random number which will be valid up to 5 second. after 5 second if anyone input that random number into system then system will say it is expire. one thing i need to mention that there will be no database interaction. i don't want to generate random number and store it in database table. 5 second validity logic will be embedded in generated random number as a result i can validate it later that whether it is generate before 5 sec or not.
i asked this question in another forum and they gave me code to generate 6 digit random number based on current date and time like
var random = new Random(System.DateTime.Now.Millisecond);
int randomNumber = random.Next(1, 500000);
is it ok? because i am not advance developer.if i can generate 6 digit random number using the above logic then later how can i determine programatically that the number was generate before 5 sec or not. basically i need two routine one for generating 6 digit random number based on current date & time and another routine will check that generated number was generated before 5 sec or not.
please guide me with concept and code. thanks
If it is the same process which stays during these five seconds, you could just store the key inside an hashtable, and to validate it check that it is inside the hashtable. Also keep in the hashtable the creation time of the key, and periodically clean that hashtable (removing obsolete keys and associated data).
Of course you'll need to generate that key either "randomly" or "cryptographically"
You can't reverse the process of random number generation to determine when the number were generated, particularly if you're seeding Random with milliseconds.
Getting a 6-digit random number:
int randomNumber = random.Next(0, 1000000);

Categories