我在c#中遇到了一个有趣的问题,我不知道该怎么办。
我需要让两首曲目在彼此之上。需要在设定的时间内播放恒定数量的哔哔声。其中一个将有一个设定的间隔(想想节拍器),但另一个需要以随机间隔播放。
我不知道如何解决第二个问题,在一段时间内以随机间隔播放一定数量的哔哔声。
答案 0 :(得分:2)
只需要花费设定的时间,将T.表示为一些足够细粒度的结构,比如毫秒。如果您需要发出N次哔声,则需要将时间间隔分割N次。因此,制作一个运行N次的循环,并在每次迭代中,在蜂鸣声的时间间隔中选择一个随机位置。根据您之后对数据所做的操作,您可能需要对嘟嘟声点进行排序。
答案 1 :(得分:0)
使用随机数生成生成总时间范围内的日期时间。当您完成随机发出哔哔声后,间隔当然是随机的。像这样:
List<DateTime> randomBeeps = new List<DateTime>();
Random rand = new Random();
for( int j = 0; j < numberBeepsNeeded; j++ )
{
int randInt = rand.Next();
double percent = ((double)randInt) / Int32.MaxValue;
double randTicksOfTotal = ((double)setAmountOfTime.Ticks) * percent;
DateTime randomBeep = new DateTime((long)randTicksOfTotal);
randomBeeps.Add(randomBeep);
}
您可能需要使用Convert.ToLong或类似的东西。不确定它是否会因为它正在舍入而在从double转换为long时出错,这很好。
答案 2 :(得分:0)
您可以将其实现为一系列一次性定时器。当每个计时器到期(或“滴答”)时,您将发出蜂鸣声,然后随机确定用于下一次计时器的持续时间。如果您选择的持续时间是1到1000(毫秒)之间的随机数,则每半秒平均一个“滴答”。
编辑:只是为了好玩,我想我会提到这是行为心理学家运行受B.F. Skinner启发的各种实验的老问题。他们有时使用称为“可变间隔”的强化计划,其中增援之间的时间在某个预定的平均间隔附近随机变化。请参阅http://www.ncbi.nlm.nih.gov/pmc/articles/PMC1404199/pdf/jeabehav00190-0145.pdf,了解有关公式的含糊探讨。
答案 3 :(得分:0)
像这样的东西应该做的伎俩(这段代码没有经过测试......但它编译得很干净)
using System;
using System.Security.Cryptography;
using System.Threading;
class BeatBox : IDisposable
{
private RandomNumberGenerator RNG;
private DateTime dtStarted;
private TimeSpan TimeElapsed { get { return DateTime.Now - dtStarted; } }
private TimeSpan Duration;
private TimeSpan BeatInterval;
private uint MinRandomInterval;
private uint MaxRandomInterval;
private uint RandomIntervalDomain;
private Timer RegularIntervalTimer;
private Timer RandomIntervalTimer;
public delegate void TickHandler( object sender , bool isRandom );
public event TickHandler TickEvent;
private EventWaitHandle CompletionEventWaitHandle;
public BeatBox( TimeSpan duration , TimeSpan beatInterval , uint minRandomInterval , uint maxRandomInterval )
{
this.RNG = RandomNumberGenerator.Create();
this.Duration = duration ;
this.BeatInterval = beatInterval ;
this.MinRandomInterval = minRandomInterval ;
this.MaxRandomInterval = maxRandomInterval ;
this.RandomIntervalDomain = ( maxRandomInterval - minRandomInterval ) + 1 ;
this.dtStarted = DateTime.MinValue ;
this.RegularIntervalTimer = null ;
this.RandomIntervalTimer = null ;
return;
}
private long NextRandomInterval()
{
byte[] entropy = new byte[sizeof(long)] ;
RNG.GetBytes( entropy );
long randomValue = BitConverter.ToInt64( entropy , 0 ) & long.MaxValue; // ensure that its positive
long randomoffset = ( randomValue % this.RandomIntervalDomain );
long randomInterval = this.MinRandomInterval + randomoffset;
return randomInterval;
}
public EventWaitHandle Start()
{
long randomInterval = NextRandomInterval();
this.CompletionEventWaitHandle = new ManualResetEvent( false );
this.RegularIntervalTimer = new Timer( RegularBeat , null , BeatInterval , BeatInterval );
this.RandomIntervalTimer = new Timer( RandomBeat , null , randomInterval , Timeout.Infinite );
return this.CompletionEventWaitHandle;
}
private void RegularBeat( object timer )
{
if ( this.TimeElapsed >= this.Duration )
{
MarkComplete();
}
else
{
this.TickEvent.Invoke( this , false );
}
return;
}
private void RandomBeat( object timer )
{
if ( this.TimeElapsed >= this.Duration )
{
MarkComplete();
}
else
{
this.TickEvent.Invoke( this , true );
long nextInterval = NextRandomInterval();
this.RandomIntervalTimer.Change( nextInterval , Timeout.Infinite );
}
return;
}
private void MarkComplete()
{
lock ( this.CompletionEventWaitHandle )
{
bool signaled = this.CompletionEventWaitHandle.WaitOne( 0 );
if ( !signaled )
{
this.RegularIntervalTimer.Change( Timeout.Infinite , Timeout.Infinite );
this.RandomIntervalTimer.Change( Timeout.Infinite , Timeout.Infinite );
this.CompletionEventWaitHandle.Set();
}
}
return;
}
public void Dispose()
{
if ( RegularIntervalTimer != null )
{
WaitHandle handle = new ManualResetEvent( false );
RegularIntervalTimer.Dispose( handle );
handle.WaitOne();
}
if ( RandomIntervalTimer != null )
{
WaitHandle handle = new ManualResetEvent( false );
RegularIntervalTimer.Dispose( handle );
handle.WaitOne();
}
return;
}
}
class Program
{
static void Main( string[] args )
{
TimeSpan duration = new TimeSpan( 0 , 5 , 0 ); // run for 5 minutes total
TimeSpan beatInterval = new TimeSpan( 0 , 0 , 1 ); // regular beats every 1 second
uint minRandomInterval = 5; // minimum random interval is 5ms
uint maxRandomInterval = 30; // maximum random interval is 30ms
using ( BeatBox beatBox = new BeatBox( duration , beatInterval , minRandomInterval , maxRandomInterval ) )
{
beatBox.TickEvent += TickHandler;
EventWaitHandle completionHandle = beatBox.Start();
completionHandle.WaitOne();
}
return;
}
static void TickHandler( object sender , bool isRandom )
{
Console.WriteLine( isRandom ? "Random Beep!" : "Beep!" );
return;
}
}