Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:40:39+00:00 2026-06-14T20:40:39+00:00

Using a random library with these functions: randomChance(p) Returns true with the probability indicated

  • 0

Using a random library with these functions:

randomChance(p) Returns true with the probability indicated by p.

randomInteger(low, high) Returns a random integer in the range low to high, inclusive.

what is the easiest way to implement a “random selector” that takes consideration of percentage, 1/4 or 1/3 etc… I got a array with key/value pairing. For example "a" migth have the value 2 and "b" have the value 2. 1/2 chance for both.

The max value will be the size of the array, cause it only contains unique items. The randomChance() function ranges between 0.0 - 1.0 where 1 = 100%. If my array size is, say 4. What is the best way of “letting 4 be 1”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:40:40+00:00Added an answer on June 14, 2026 at 8:40 pm

    Lets say you have:

    a = 2, b = 2, c = 1, d = 3
    

    now make it:

    a = 2, b = 4, c = 5, d = 8
    

    Create a random number from 1 to MaxVal (value of the last key, 8 in this example). Select the first Key where Value >= RandomNum

    EDIT

    I made a small VB.Net to show the algorithm and how it works. The code is not meant to be: Good, elegant, performant or readable.

    Module Module1
    
        Private Class Value
            Public vOrg, vRecalc, HitCount As Integer
            Public Key As String
            Public Sub New(s, v1, v2, c)
                Key = s : vOrg = v1 : vRecalc = v2 : HitCount = c
            End Sub
        End Class
    
        Sub Main()
    
            ' set initial values
            Dim KVP() As Value = {New Value("A", 2, 0, 0),
                                  New Value("B", 2, 0, 0),
                                  New Value("C", 1, 0, 0),
                                  New Value("D", 3, 0, 0)}
            ' recalc values
            For i = 0 To KVP.Length - 1
                If i = 0 Then KVP(0).vRecalc = KVP(0).vOrg Else KVP(i).vRecalc = KVP(i).vOrg + KVP(i - 1).vRecalc
            Next
            ' do test
            Dim r As New Random
            Dim runs As Integer = 1000 * 1000, maxval As Integer = KVP(KVP.Length - 1).vRecalc
            For i = 1 To runs
                Dim RandVal = r.Next(1, maxval + 1)
                Dim chosen As Integer = (From j In Enumerable.Range(0, KVP.Length) Where KVP(j).vRecalc >= RandVal Take 1 Select j)(0)
                KVP(chosen).HitCount += 1
            Next
            ' ouput results
            For Each kv In KVP
                Console.WriteLine("{0} was chosen with {1:F3} propability, expected was {2:F3}", kv.Key, kv.HitCount / CDbl(runs), kv.vOrg / CDbl(maxval))
            Next
    
            Console.ReadLine()
    
        End Sub
    
    End Module
    

    An output sample:

    A was chosen with 0.250 propability, expected was 0.250
    B was chosen with 0.251 propability, expected was 0.250
    C was chosen with 0.124 propability, expected was 0.125
    D was chosen with 0.375 propability, expected was 0.375
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m generating multiple, random sized, circular elements using the Raphael JavaScript library but because
How to generate random numbers using assembly code in the range 1--4 ?
I need to generate a lot of random numbers. I've tried using random.random but
I am using large random numbers as keys (coming in from another system). Inserts
Simple enough question: I'm using python random module to generate random integers. I want
Using the Xorshift random number generator... I already have the generator, but I haven't
I'm using srandom() and random() to generate random numbers in c on a Unix
I'm using a custom random number function rand48 in CUDA. The function does not
I am creating a random ID using the below code: from random import *
I actually want to generate random password using PHP uniqid() function $randomPassword = uniqid();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.