|
Random r = new Random();
int key = r.Next(0, 10000);
int prime = r.Next(key, key * 999);
int a = r.Next(1, prime - 1);
int b = r.Next(0, prime - 1);
Console.WriteLine(key);
Console.WriteLine(prime);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine( ((key*a + b)%prime) % 1000);
Console.ReadLine();
For example:
In google: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = 563}
In c#: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = -732
|
|
|
|
|
Quake2Player wrote: 8 568 * 4 974 445)
using 32-bit signed arithmetic (as in int) this results in an overflow, that is where the negative stuff is coming from. Nothing wrong with the modulo operator.
test: declare long variables, initialize them, and use those for your expression, instead of numeric constants. (decimal should work too).
|
|
|
|
|
Beat me to it!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
|
It's because of the overflow in the first part of the calculation. Make one of the values a double and you get the correct result.
(((8568 * 4974445d) + 2820002) % 7661773) % 1000)
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
How can I use long if I'm generating random numbers???
This is all about the UNIVERSAL HASHING FUNCTION:
<code>
public int Evaluate(int key)
{
do {
randomPrime = rnd.Next(10001, 10000*999);
}
while (!IsPrime(randomPrime));
this.randomA = rnd.Next(1, randomPrime - 1);
this.randomB = rnd.Next(0, randomPrime - 1);
return ((randomA * key + randomB) % randomPrime) % tableLength;
}
</code>
Notes:
-10000 is the maximum possible key
-tableLength is 1000.
-randomA, randomB and randomPrime are ints
As Im getting negative numbers Im trying
return Math.Abs(((randomA * key + randomB) % randomPrime) % tableLength);
But its not giving me fast results in the hash table
modified on Saturday, August 29, 2009 6:01 PM
|
|
|
|
|
Hello. I just want to know if its possible for me to store the data inside an array to a text file and also if its possible to transfer all the data in the text file into an array?
|
|
|
|
|
Yes - Its possible
|
|
|
|
|
Ok..Thanks..
|
|
|
|
|
As Coding says, yes it is possible. How you do it will depend on what kind of info you have in your array, and what you want to do with the text file.
If it is an array of strings, easy peasy.
If it is an array of something else, then slightly harder - you will either have to do the work yourself or use a Formatter - XML, Binary or Soap.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
oh..ok then..thanks for the info..
|
|
|
|
|
Sorry, that was a little unhelpfull!
For an array of strings, look at File.WriteAllLines[^]. To read it back, use ReadAllLines
For an array of something else, you will need to:
1) Open file
2) Set up a loop through each element of the array
3) For each element, use ToString member, or string.Format to create a string
4) Write the string to the file
5) Close the file.
To read them back, use the Parse method after reading the line.
or look at BinaryFormatter, XMLFormatter and SoapFormatter in the MSDN, but these will not necessarily create a file you want to look at, or edit by hand!
Good luck!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
TMI
the first "yes" was the best answer
|
|
|
|
|
I thought so - it was an accurate and complete answer - but not so helpful to a beginner!
That's why I apologised and gave more info.
I'm trying to obey the new rules...but some of these questions do make it difficult.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Hello everyone,
I need help,
How can I change "visited links color" in richtextbox ? - C#
I mean, when I click a link in richtextbox, clicked link opens an ie page.
But clicked link color doesn't change... ( I want to change it's color )
Please help me,
Best Regards.
|
|
|
|
|
nobody ???
is it impossible ?
|
|
|
|
|
I have a textBox databound to a datatable via the properties window. It works well.
I now need to remove this databinding. When doing this, via the properties window, and setting the Text section of DataBindings back to 'None' I get this error when re-running the solution:
'object reference not set to an instance of an object' for line:
firstPaymentDateTextBox.DataBindings["Text"].Format += new ConvertEventHandler(DebtConvert.DateToString);
it seems that VS 2008 does not remove this line when resetting the databinding via the designer properties window. It there a way around this please? As I have a lot of databindings to remove it seems a little tedious for such a, otherwise, powerful IDE.
|
|
|
|
|
Why do you need to remove the bindings?
|
|
|
|
|
The project has changed and the data binding for the textbox is no longer required.
|
|
|
|
|
I have an image of more than 2 megabytes that can not open in any program,
so I need to resize it so that any program can open if I could carry it in the program would be easy but it has an error OUT OF MEMORY someone tell me how to resize the image without loading in C Sharp
|
|
|
|
|
Hi,
an OOM exception does not always really mean you ran out of memory! Look here[^].
I suggest you first validate your image file by opening it succesfully with one of the reputed image processing programs, such as Adobe Photoshop.
|
|
|
|
|
I agree with Luc - I regularly work with images much bigger than 2Meg without problems in C#. I would suspect your code is at fault - try it with a small test image, and it will probably give the same error.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
The problem is not in my code is on the image that is a bmp and is a large map and i tried to open the image with Irfanview with Quantum gis and with the Gimp and the Windows can not read the image data. I want to resize the image from the file system and not from the image that appears on the screen.
I have not harmed any tree to write this message but have produced a lot of smoke trying to resize the image.
Thank's
|
|
|
|
|
Since nothing is opening it, it would appear that the image file is corrupted or otherwise not in an expected and supported format.
Just for giggles, what are the dimensions of the image, in pixels, and the color depth?
|
|
|
|
|
The windows can not give me this data but I made a program that gave me
height= 25163 px
width = 38180 px
rate of bits = 24
|
|
|
|