|
Compile it as a resource within an assembly?
|
|
|
|
|
|
Yes. A good memory. Also need a working example as demo with my thanks in advance to sender.
modified on Wednesday, August 19, 2009 11:23 AM
|
|
|
|
|
I don't think people will send you a demo or code, but you can try Googling encrypting and C. You will be surprised at how easy it will be to get code from Google that nearly suits your entire model, with a need for only slight modification.
|
|
|
|
|
Thanks Eliott for advice on encryption . If feasible, prefer embedding txt file where application path is a difficulty. Its a freeware for my native community but I like to protect my translation work from duplication. I can send said work for a solution...
|
|
|
|
|
Hello.
I'm having a little problem with the Random() class generating the same number. Only the first index is being validated and the rest has the same number.
so far this is my code:
using System;
using System.Collections.Generic;
using System.Text;
namespace LotteryGenerator
{
class RandomNumberGenerator
{
Random randomGenerator;
int[] randomStorage = new int[6];
public RandomNumberGenerator()
{
}
public int RandomGenerator(int randomNumber)
{
for (int a = 0; a < randomStorage.Length; a++)
{
for (int aa = 0; aa < 6; aa++)
{
randomGenerator = new Random();
randomNumber = randomGenerator.Next(1, 20);
randomStorage[aa] = randomNumber;
if (randomStorage[a] == randomNumber)
{
randomNumber = randomGenerator.Next(1, 20);
randomStorage[a] = randomNumber;
}
else
{
}
}
Console.WriteLine("{0}", randomStorage[0]);
}
return randomNumber;
}
}
}
|
|
|
|
|
Try calling new Random() just once, in your constructor. It could be that every time you create a new one it resets the seed.
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
Your right...when i transferred it outside the for loop it stops pushing the same number. But there are still two same numbers that are in the array..maybe changing something in the validation could solve the problem.
Thanks.
|
|
|
|
|
Random means that sometimes you will get the same number twice!
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
Ok then..I understand...if that's the case then i should really do something to prevent that..
|
|
|
|
|
If you don't supply a seed value to the RNG when you create it, it uses the current timer value, down the the millisecond it supplies, as the seed. Since your code (needlessly) creates a new RNG every interation of the loop, multiple iterations are using the same timer value as their seed, and hence, comming up with the same stream of "random" numbers.
All you do is create the RNG once, in the class' constructor or some other appropriate place that is executed only once. You don't need to create a new RNG every time you need a new random number.
Also, the RNG does not guarantee that each number in the set of returned number will be unique. You'll have to wrap it in an implementation that keeps track of the numbers already returned and check to see if the next number returned is already in the set and grab a new number if it is. This will, of course, get slower and slower as the number of unique numbers you use gets larger and larger.
|
|
|
|
|
If your application requires different random number sequences, invoke this constructor repeatedly with different seed values.
Give the constructor if the Random class a seed. Any changing number will do, e.g. the sum of the hours and minutes of the current date.
Enjoy
Where?
|
|
|
|
|
Ok. I'll post the code that still stores two same numbers into the array.
Here's the code:
using System;
using System.Collections.Generic;
using System.Text;
namespace LotteryGenerator
{
class RandomNumberGenerator
{
Random randomGenerator;
int[] randomStorage = new int[6];
public RandomNumberGenerator()
{
}
public int RandomGenerator(int randomNumber)
{
randomGenerator = new Random();
for (int a = 0; a < randomStorage.Length; a++)
{
for (int aa = 0; aa < 6; aa++)
{
randomNumber = randomGenerator.Next(1, 50);
randomStorage[aa] = randomNumber;
if (randomStorage[aa] == randomNumber)
{
randomNumber = randomGenerator.Next(1, 50);
randomStorage[aa] = randomNumber;
if (Array. )
{
randomNumber = randomGenerator.Next(1, 50);
randomStorage[aa] = randomNumber;
}
}
else
{
}
}
Console.WriteLine("{0}", randomStorage[0]);
}
return randomNumber;
}
}
}
Please leave a comment about what should i do to solve the minor problem. I'm not asking for the codes as i'll be the one to do it. Thanks.
|
|
|
|
|
Don't instantiate the Random in the method!
class RandomNumberGenerator
{
Random randomGenerator<big> = new Random()</big>;
or, better yet
class RandomNumberGenerator
{
private static readonly Random randomGenerator<big> = new Random()</big> ;
|
|
|
|
|
Ok..Thanks
One question, what if i deleted readonly? Will there be any changes?
|
|
|
|
|
|
Given you are writing a lottery number generater, there is an easier implmenation to select numbers:
1. Create a list of int, containing the numbers in order you want to select from (e.g. 1-50)
2. Create a random position number (starting in the range 0-49 in my example),
3. Take the value at the random position and remove from the list
4. Reduce the random position range by one as you have removed from the source list
5. Rinse and repeat steps 3 & 4 until you have enough numbers to fill your lottery card...
You can refactor this to work without the list, but it will be harder to understand
|
|
|
|
|
|
The seed is the number that you pass in the constructor. E.g.,
randomGenerator = new Random(1); will result in a different list of "random" numbers than this;
randomGenerator = new Random(2); Enjoy
Why?
|
|
|
|
|
This random in not real random function.
long ago i tried to implement random with a seed based on timer (milliseconds), i don't remember what solution exactly i had, i think it was ATL vector solution. I found somethink that might itnrest you:
Random sort order in C# or LINQ[^]
|
|
|
|
|
|
I came to C# from C++ and an curious as to why they chose to use "\r\n" for newlines instead of "\n" - anyone know?
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
\r = carriage return
\n = new line equivalent of line feed.
ie Carriage Return / Line feed.
I *think*!
|
|
|
|
|
Yes but C++ managed with just \n - I'm curious as to why they changed to \r\n in C#.
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
It's not language-dependent. A single "\n" is unix-style, but Windows wants a "\r\n" to denote a newline. Mind you that "\n" is also treated as a newline, but it's not the convention.
Meaning that if you search an ASCII-text under Windows for newlines, you'd typical be looking for the "\r\n" combination. If you only replaced the "\n" character, a "\r" would remain.
Whot?
|
|
|
|