Jump to content

Hi guys,

 

So down in the next line I will give you all information you need.

 

Language(also if kinda easy to see): C#

 

What Am I doing: I am making a method, this method is going to create random numbers wich will be stored in an Array, but if the number is already used, then a new random number should be created, so no number is in the Array twice.

 

Where Am I stuck: I know how to get the Random Numbers into the Array, but I do not know how I know can make it so every number only is in once. What I was thinking off is checking every time something is getting written into a Array and then check it with the Array's before if they are equal. And if they are equal a new Random Number gets generated and the current space in the Array will be overwritten and after that it will be checked again with the previouse to make sure the new Random number is again not one that was used already. But this would me for me to make alot of for's and I am not sure how to do that got the kinda basic idea but not how to do it :D

 

Code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Random_Group_generator_Konsolenanwendung{    class Program    {        static int[] Groups;        static string[] Names;        static int AmmountofPeople = 0;         //Will be changed by user.        static Random Random = new Random();        static int[] Randomorder;                    .                    .                    .                    .                    .                    .                    .                    .                    .                    .                    .                    .                    .                    .        static void RandomGenerator()        {            for(int i = 0; i <= AmmountofPeople; i++ )            {                Randomorder[i] = Random.Next(0, AmmountofPeople);            }        }    }} 

 

If you got any questions about code or not understanding what I am doing pls let me know. Appreciate your help guys, hope you can help me out :D

 

@Nuluvius

 

 

Nice Greets,

Rockilocky

Link to comment
https://linustechtips.com/topic/290828-help-with-programm-i-am-making/
Share on other sites

Link to post
Share on other sites

As always in programming, it helps to break down a larger problem into atomic problems. Create a separate procedure for checking if a number is in a given array and it should become clear how that will fold into what you are doing.

 

Take note that this method will result in your program having poor time complexity on the order of N2 because you will need to walk the whole array every time you add a new element.

Link to post
Share on other sites


if(Group.Contains(Random))

{

//from here you should know what to do

}

CPU: Intel Core i5 4690K @ 4.6Ghz CPU Cooler: Noctua NH-D15 GPU: GTX 1070 TI RAM: Crucial Ballistix Tactical 16GB (4x4) Mobo: ASUS Z97-PRO(Wi-Fi ac) PSU: Corsair RM Series RM750 Case: Fractal Design Define R4 no window

Link to post
Share on other sites

why do you not want any number to appear more then once?? because if no then you haven't got a random number, (dont know C just wondering)

I want the numvers also if they only appear once to be in a random order in the array, and the only way I know in C# right now is to just make a Random variable that changes itselfs always when you call its function so the array is filled in a random order with these numbers. BTW I need this fo my Random Group Generator Project :D

Link to post
Share on other sites

As always in programming, it helps to break down a larger problem into atomic problems. Create a separate procedure for checking if a number is in a given array and it should become clear how that will fold into what you are doing.

 

Take note that this method will result in your program having poor time complexity on the order of N2 because you will need to walk the whole array every time you add a new element.

One question on that if you don't mind: N² heard of it, but not sure what it is could you explain it?

 

2nd question: Don't you know of a simplier way to do this without really having to shit on the time then?

Link to post
Share on other sites

One question on that if you don't mind: N² heard of it, but not sure what it is could you explain it?

 

2nd question: Don't you know of a simplier way to do this without really having to shit on the time then?

 

Sure. The "N" refers to the problem size, in this case the number of people. The whole N2 refers roughly to how your solution scales in the terms of the number of operations that your program performs. In this case you are performing N operations N times.

 

A possible strategy would be to iterate over the array once, generate a new position for the element and swap it into that position.

Link to post
Share on other sites

Take note that this method will result in your program having poor time complexity on the order of N2 because you will need to walk the whole array every time you add a new element.

Don't you know of a simplier way to do this without really having to shit on the time then?

the program could potentially even run forever if the random function keeps returning the same number

the more numbers you try to generate, the slower the function will be bcause it will find more "collisions", and will just keep extracting and checking numbers over and over just to generate one

 

what do you need this thing for? is it acceptable to generate random numbers in a less random way, like every new random number is greater than the last one?

all the methods i can think of to speed up the random distinct number generation take away a bunch of memory

Link to post
Share on other sites

the program could potentially even run forever if the random function keeps returning the same number

the more numbers you try to generate, the slower the function will be bcause it will find more "collisions", and will just keep extracting and checking numbers over and over just to generate one

 

what do you need this thing for? is it acceptable to generate random numbers in a less random way, like every new random number is greater than the last one?

all the methods i can think of to speed up the random distinct number generation take away a bunch of memory

 

So what I am trying to do is a a Random Group Generator. So basicly it starts with asking you how big your biggest group is. for example 2.

 

Then it asks you how many groups of 1 you have:

and how many groups of 2 you have:

 

after that it calculates the maximum ammount of people there can be for example now 3.

 

Then you input 3 names

and now these names should be randomly set into those groups thats why random.

 

 

if you have a more efficient way I would aprreciate the help.

 

 

Whole code if needed

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Random_Group_generator_Konsolenanwendung{    class Program    {        static int[] Groups;        static string[] Names;        static int AmmountofPeople = 0;        static Random Random = new Random();        static int[] Randomorder;        static void Main(string[] args)        {            int AmmountofPeopleHighestGroup;            Console.WriteLine("Welcome to the Random Group Generator!");            Console.Write("Up to how many members should there be in the highest group: ");            AmmountofPeopleHighestGroup = int.Parse(Console.ReadLine());            Console.WriteLine(" ");            MemberofGroup(AmmountofPeopleHighestGroup);            InputofMembers(AmmountofPeopleHighestGroup);            RandomGenerator();            Console.ReadKey();        }        static void MemberofGroup(int AmmountofGroups)        {            Groups = new int[AmmountofGroups +1];            //For Loop to let the User type in how many Groups of wich he has.            for (int i = 1; i <= (AmmountofGroups); i++)            {                Console.Write("How many Groups of " +i +" do you want: ");                Groups[i] = int.Parse(Console.ReadLine());            }        }        static void InputofMembers(int AmmountofGroups)        {            Console.WriteLine("Geben Sie bitte die Namen der Personen ein: \n");            for (int i = 1; i <= AmmountofGroups; i++ )            {                AmmountofPeople = AmmountofPeople + Groups[i] * i;            }            Names = new string[AmmountofPeople + 1];            for (int i = 1; i <= AmmountofPeople; i++ )            {                Console.Write("Geben Sie den Namen der " + i + " ein: ");                Names[i] = Console.ReadLine();            }        }        static void RandomGenerator()        {            for(int i = 0; i <= AmmountofPeople; i++ )            {                Randomorder[i] = Random.Next(0, AmmountofPeople);                }            }        }    }} 

Link to post
Share on other sites


static void RandomGenerator()

{

for(int i = 0; i <= AmmountofPeople; i++ )

{

int temprandom = Random.Next(0, AmountofPeople);

while(Randomorder.Contains(temprandom))

{

temprandom = Random.Next(0, AmountofPeople);

}

Randomorder = temprandom;

}

}

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to post
Share on other sites

For each element in the array you want to randomize, generate a random number between 0 and the size of the array less one and swap the element into that position.

 

Sure you could end up with the same array you had when you started, but that is the nature of randomness.

Link to post
Share on other sites

as @SSL said, you could just insert the names in order and then shuffle the array

One question though, tried to use a shuffle function but this one does not work any help:

using System;class Program{    static void Main()    {	string[] arr = new string[]	{	    "cat",	    "animal",	    "abacus",	    "framework"	};	string[] shuffle = RandomStringArrayTool.RandomizeStrings(arr);  //RandomStringArrayTool is red underlined	foreach (string s in shuffle)	{	    Console.WriteLine(s);	}    }}
Link to post
Share on other sites

 

One question though, tried to use a shuffle function but this one does not work any help:

using System;class Program{    static void Main()    {	string[] arr = new string[]	{	    "cat",	    "animal",	    "abacus",	    "framework"	};	string[] shuffle = RandomStringArrayTool.RandomizeStrings(arr);  //RandomStringArrayTool is red underlined	foreach (string s in shuffle)	{	    Console.WriteLine(s);	}    }}

 

Because that class doesn't exist in yor program.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×