随机名称和一天中的小时 [英] Random Name and hour of day

查看:84
本文介绍了随机名称和一天中的小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是:



我们有一个人员列表,一天的列表和一天300分钟的范围。

我们需要在一天一分钟内为一个人创建一个随机条目,例如:



The situation is:

We have a list of people, a list of days and a range 300 minutes a day.
We need to create a random entry for a person within a day and a minute, example:

Person List
ID     Name
1      John
2      Sophia
3      Lydia







Days List
ID     Description
1      Monday
2      Tuesday
3      Wednesday







Hours Range (300 minutes daily)
8:30 AM - 11:30 AM
1:00 PM -  3:30 PM





我们需要将此条目随机计数3300次,考虑到同一个人不能有相同的分钟和日期,例如





We need to make this entry random with a count of 3300 times, taking account the same person cannot have a same minute and day, example

Name     Day     MinuteNumber
John,     Monday,    2(8:32AM)
Sophia,   Wednesday, 299(3:29PM)
Lydia,    Tuesday,   279(3:09PM)
Lydia,    Tuesday,   279(3:09PM) (This cannot happen)
Lydia,    Monday,    279(3:09PM) (This can happen)





我想在VB.NET中这样做,但是C#也是受欢迎的,到目前为止我正在尝试使用这段代码如果找到的解决方案也会在这里发布:





I''m trying to do this in VB.NET, but C# its welcome too, so far i''m trying with this piece of code and if solution its found will post here too:

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        ' by making Generator static, we preserve the same instance '
        ' (i.e., do not create new instances with the same seed over and over) '
        ' between calls '
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function

推荐答案

嗨Rhapemerald,



您可以简单地检查碰撞,如果是,则获取下一个随机值:

**我不会在GetRandom函数中使用New System.Random(),更好的是使用类似的东西:



Hi Rhapemerald,

You may simply run a check for collisions and while true, get the next random value:
** I would not use "New System.Random()" inside the GetRandom function, much better to use something of the sort:

static Random rnd = New Random();
public GetRandom(int min, int max)
{
    return rnd.Next(min, max);
}





您可以根据需要添加值以避免:





You can add values to avoid if you want:

public GetRandom(int min, int max, List<int> excludeValues)
{
    int tmp = rnd.Next(min, max);
    while (excludeValues.Contains(tmp)
        tmp = rnd.Next(min, max);

    return rnd.Next(min, max);
}





祝你好运,

Edo



Good luck,
Edo


基于我的提示从甲板上随机抽取5张卡片 [ ^ ]这里你是一个用于选择范围内唯一随机数的类( min max 包括)。

Based on my tip "Random extraction of 5 cards from a deck"[^] here you are a class for selecting unique random numbers in a range (min and max included).
public class UniqueRand
 {
   int[] data;
   int cur;
   Random r;
   void init(int min, int max)
   {
     data = new int[max - min + 1];
     r = new Random();
     cur = data.Length;

     for (int i = 0; i < data.Length; i++)
       data[i] = min + i;
   }
   public UniqueRand(int min, int max)
   {
     init(min, max);
   }
   public UniqueRand(int max)
   {
     init(0, max);
   }
   public int next()
   {
     if (cur == 0) throw new Exception("UniqueRand exhausted");
     int index = r.Next(cur);
     cur--;
     int result = data[index];
     data[index] = data[cur];
     data[cur] = result;
     return result;
   }
 }





一个小小的测试(显示它,正确地,投掷......) />



A little test (showing also it, rightly, throwing...)

public static void Main()
    {
      UniqueRand ur= new UniqueRand(10,20);
      for (int i=0; i<=11; i++)
      {
        Console.WriteLine(ur.next());
      }
    }


我们找到了一种方法,使用
We found a way to make our random seed more complex with the
RNGCryptoServiceProvider



我们制作了这样的函数:


We made a function like this:

Public Function randomSelect(ByVal min As Integer, ByVal max As Integer) As Integer
        Dim rng As New RNGCryptoServiceProvider()
        Dim buffer As Byte() = New Byte(3) {}
        rng.GetBytes(buffer)
        Dim result As Integer = BitConverter.ToInt32(buffer, 0)
        Return New Random(result).[Next](min, max)
    End Function



随着种子随机,它提供更纯粹的随机性


With the seed being random it provides a more pure randomness

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Collections.Generic
Imports System.Security.Cryptography

Public empList As List(Of Integer)
Public daysList As List(Of DateTime)

Public Sub generateRandom()
     Dim tecnInDaysHash As New HashSet(Of TecnInDays)
     Dim randEmployeeIndex, selectedEmpId As integer
     randEmployeeIndex = randomSelect(1, empList.Count)
     randDayIndex = randomSelect(1, daysList.Count)
     selectedEmpId = empList.Item(randEmployeeIndex)
     selectedDayDate = daysList.Item(randDayIndex)
'Validate duplicates, if any get new random record
                Dim tinObj As New TecnInDays
                tinObj.Days = selectedDayDate.Day
                tinObj.IdTecn = selectedEmpId
                If Not (tecnInDaysHash.Add(tinObj)) Then
                    randEmployeeIndex = randomSelect(1, empList.Count)
                    selectedEmpId = empList.Item(randEmployeeIndex)
                End If
End Sub





我们创建一个小类来识别重复项



We create a little class to identify duplicates

Imports Microsoft.VisualBasic

Public Class TecnInDays
    Private _IdTecn, _Days As Integer

    Public Property IdTecn() As Integer
        Get
            Return _IdTecn
        End Get
        Set(ByVal value As Integer)
            _IdTecn = value
        End Set
    End Property
    Public Property Days() As Integer
        Get
            Return _Days
        End Get
        Set(ByVal value As Integer)
            _Days = value
        End Set
    End Property
End Class





希望这有助于任何偶然发现此事的人。



Hope this helps to anyone that stumble upon this.


这篇关于随机名称和一天中的小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆