Random Arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Random Arrays

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    Random Arrays

    Hello,

    I am working on a project for fun. What I am trying to do is create a C# version of the game Deal or No Deal.

    Currently, I am struggling with the concept of the briefcases. What I would like to do is create an array of size 26 (there are 26 total briefcases) with values between $0.01 and 1M (just like on the game show).
    This way, the case number can be the index of the array element and the values can be any value between $0.01 and 1M.

    So, my question is: Is there a way to create an array that fills randomly with a predefined set of values?

    Or does anyone think it would be easier to create a "briefcase" object with attribute of number and value?

    Thanks for any advice,
    Bill

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Random Arrays

    Put the values in the array in order.

    Then write a loop which executes 1 zillion times. Each time around the loop pick 2 random numbers between 0 to 25 inclusive and swap the array values at those index's.

    A bit like shuffling cards...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Random Arrays

    You don't need to swap numbers millions of times. The algorithm you are looking for is called Fisher–Yates shuffle and is quite efficient (check for the modern algorithm).

    Hope it helps

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Random Arrays

    ok, a zillion was a bit of an exagg I think the way I suggested maps more to the real world than FYS.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center