0

I am trying to create a Bingo Game for an Elderly home. I was able to make one using random number generators that are used to scan an array but the problem is that it takes to long(2 min) to search the array and ensure no doubles are repeated. I did some research and found something called pseudo random in c that does not repeat numbers upon creation. But the code looks very convoluted so I do not feel comfortable taking something that I do not fully understand. Which brings to my question which is there a simple way to create a non-repeating random number generator or a good explanation that could explain one?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • How many numbers are you generating exactly? It shouldn't take that long even if you use the "scan the array for duplicates" approach. – JJJ Nov 16 '14 at 16:52

5 Answers5

4

Long ago, I wrote this very simple program in C programming language that generate 10 random numbers between 0 to 20 without repeating any number.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10      //Number to be generated.
#define rangeMAX 20 //Upper limit of range.
#define rangeMIN 0  //Lower limit of range.

int main()
{
  int get, c, i, arr[MAX], chk, x;
  c = i = 0;
  srand(time(0)); // this will ensure that every time, program will generate different set of numbers. If you remove this, same set of numbers will generated every time you run the program.
  while(c < MAX)
  {
    get = ((rand() % (rangeMAX-rangeMIN+1)) + rangeMIN); // generate random number.
    //After generating that number check if it is already in array.
    for(i=0; i<=c; i++)
    {
        if(arr[i] == get)
        {
            chk = 0;
            break;
        }
        else if(arr[i] != get)
        {
            chk = 1;
        }
    }
    if(chk==1)
    {
        arr[c]=get;
        printf("%d\n",arr[c]);
        c++;
    }
  }
  return 0;
}

Hope this will help.

Anshuman
  • 758
  • 7
  • 23
0

Fill out you array with appropriate numbers then shuffle it.

Denis Borovikov
  • 737
  • 5
  • 9
0

Create an array with N elements where each element hold the same value as it's index like this: arr[0] = 0, arr[1] = 1, ...

the values in the array may be whatever you like(not repeating of course).

Then just shufle the array by looping through the array and swapping the current element with one at random position.

Now that your array is shuffled, you just maintain an index starting from zero which holds the index of the current random number. When you want a new random number, lookup the array value at the current index and then increment the current index.

If your index reaches the end of the array you can just reshuffle the array and set the current index back to zero.

Dimitris Fousteris
  • 1,091
  • 8
  • 12
  • Wow. Thank you. Very detailed. I do not quite understand the random position will not repeat. Once again what is this shuffle. Again I appreciate the help. – Eric Cacciavillani Nov 16 '14 at 19:11
  • "shuffle" is what you do to randomize a deck of cards, just before you deal them. Here you do the same to a list of unique values that is sorted at first, but that you want to "unsort". – Hans Kesting Nov 24 '17 at 13:53
0

There are two ways of ensuring that randomly drawn numbers do not repeat:

  • remembering which numbers have been seen already, so that duplicates can be rejected
  • drawing the numbers from a sequence that is free of repetition

Which method is more suitable depends on the circumstances; both can be simple to implement and very fast, neither is universally applicable or universally 'better'.

The second method - using repetition-free sequences - is the simpler of the two; in extremis it could be implemented in one or two lines of code. It was discussed just recently over on Code Review in the topic

As regards the speed of your duplicate search: I am certain we can speed it up by a couple orders of magnitude, should you decide that it is the method you prefer.

Community
  • 1
  • 1
DarthGizka
  • 4,347
  • 1
  • 24
  • 36
-2

Bingo Game VB (add 2 buttons called btnNumber and BtnShuffle, add textbox on form design) (change 100 to 75 or any number)

Public Class Form1 Dim randomarray(100) As Integer Dim Index As Integer = 1 Dim Counter As Integer Dim btnNumber As System.Windows.Forms.Button Private Sub Btnshuffle_Click(sender As Object, e As EventArgs) Handles Btnshuffle.Click For i = 1 To 100 randomarray(i) = i

    Next
    Randomize()
    Dim j As Integer
    Dim tmp As Integer

    For i = 1 To 100 - 1
        j = Int((100 - i + 1) * Rnd() + i)
        tmp = randomarray(i)
        randomarray(i) = randomarray(j)
        randomarray(j) = tmp

    Next
    BtnNextNumber.Enabled = True
    Index = 1
    TextBox1.Clear()
End Sub

Private Sub BtnNextNumber_Click(sender As Object, e As EventArgs) Handles BtnNextNumber.Click

    TextBox1.Text = randomarray(Index)
    Index = Index + 1
    If Index = randomarray.Count Then
        BtnNextNumber.Enabled = False

    End If
    Me.Controls("btnnumber" & TextBox1.Text).BackColor = Color.Yellow


End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Me.Counter = 1 To 100
        btnNumber = New System.Windows.Forms.Button
        btnNumber.Name = "btnNumber" & Counter
        btnNumber.Text = Counter
        btnNumber.Font = New System.Drawing.Font("Arial", 10.0!, System.Drawing.FontStyle.Bold)
        btnNumber.BackColor = Color.White
        'btnNumber.ForeColor.White
        btnNumber.TextAlign = HorizontalAlignment.Center
        btnNumber.Left = 40 + ((Counter - 1) Mod 10) * 60
        btnNumber.Top = 50 + ((Counter - 1) \ 10) * 40
        btnNumber.Width = 60
        btnNumber.Height = 40
        btnNumber.Enabled = False

        Me.Controls.Add(btnNumber)
    Next

End Sub

End Class

samy
  • 1
  • 1
  • How is "(change 100 to 75 or any number)" relevant to any part of the question? Is "Button1.Enabled = True Index = 1 TextBox1.Clear()" actually necessary? – rudolf_franek Nov 24 '17 at 13:12
  • This three year old question lacks the `C` tag, but it's apparent from the question's title. Your answer also lacks any explanation of why this would be a good approach. It appears to be a Fisher-Yates shuffle? – CodeCaster Nov 24 '17 at 13:59