Random number pickup

Just starting out? Need help? Post your questions and find answers here.
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Random number pickup

Post by bidanh00co »

I want to make a small apps that can pick a random number. For i.e, there are 80 people in a class. We want to divide them in to 8 group randomly. I can do like this (I post the simplest code)

NumberOfPeople=80
NumberOfGroup=8
Dim Index(NumberOfPeople)

file=CreateFile(0, "c:\list.txt")
If file
For i=1 to NumberOfPeople
Index(i) = Random(NumberOfPeople)
If Index(i) <> 0
WriteStringN(Str(Index(i)))
EndIf
Next i
CloseFile(0)

-----------
Everything seems simple, but the matter is some generated number will be duplicated to triplicated.

I don't know how to avoid this. It mean how to Random a value but not the same generated value.
Reality, as you see, the task is divide into NumberOfGroup. So, it will be wrong if there are 2 or more same number in many group.

Extensively, I want to link this thing to Database. Despite generate number, it will generate Name of the People.

Would you please help me or give me a way ? It's great if you help me with Database link.
Splondike
New User
New User
Posts: 6
Joined: Mon Aug 01, 2005 1:47 pm

Post by Splondike »

As I understand it, you're looking to randomise the order of the numbers 1 to 80 (using your example). Eg. Using the numbers 1 to 4:

What you're getting:
3 3 1 2
What you want:
4 1 3 2

Here's a bit of code which should help you -i'm not going to worry about writing to a file, just assigning an array-:

Code: Select all

NumberOfPeople = 80
Dim Index(NumberOfPeople - 1) ;Remember, arrays start at 0, so array will go from 0 to 79

For i = 1 to NumberOfPeople
    r = Random(NumberOfPeople) ;Assign r a value between 0 and 79
    If Index(r) = 0 ;Default value, unassigned array element
        Index(r) = i
    Else
        i = i - 1 ;Keep the value of i the same in the for loop; we picked an array element which was previously assigned
    EndIf
Next i
You'll need to change the above code a bit (obviously :wink:), it mightent work as is, it was just hacked up to give you the idea.

Regarding the database, can't help you unfortunately, i'm new at this thing myself :).
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Post by bidanh00co »

oh Splondike, it doesn't work exactly what I need. Did you test your code ?? :)
ravagepn
User
User
Posts: 10
Joined: Sun Mar 06, 2005 3:54 pm

Post by ravagepn »

Really it doesn't work, but I got the point of what he meant to do. Also, check this topic about linkedlists. It may help you:
viewtopic.php?t=16135
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Post by bidanh00co »

Thank you !

But Is there any way to re-order Linkedlist randomly ?

Example: you have a list contains numbers from 1 to 80
Is there a way to random (or re-order that numbers Randomly: 45,25,68,11,12,1,3,6,79,1,5,9,90,15.....
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Post by Fou-Lu »

I think this might work:

Code: Select all

Dim people(80)

for n=0 to 80
people(n)=n ;first we create an ordered list
next n

;now we make a mess!!!

for n=0 to 80
m=random(80)
temp=people(m)
people(m)=people(n)
people(n)=temp
next n
Got it? :wink:

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Here is one way :)

Code: Select all

Structure grpDef
  names.s
  ctr.l
EndStructure

NumPeeps = 82                          ; Total People
SzGrps = 4                             ; People per group

NewList allPeeps.s()                   ; List of the people to be grouped
NewList fullGrp.grpDef()               ; List of  teams or groups

For i=1 To NumPeeps                    ; Populate our people list somehow
  AddElement(allPeeps())               ; Here we just use numbers
  allPeeps()="Name_"+Str(i)
Next

ResetList(allPeeps())
AddElement(fullGrp())                  ; Create the first group

While CountList(allPeeps())<>0
  g=Random(CountList(allPeeps())+1)    ; Get a random number 0 to remaining people+1
  For i=0 To g                         ; Advance through the working group
    NextElement(allPeeps())            ;     by random number of places.
    If ListIndex(allPeeps())=CountList(allPeeps())-1
      ResetList(allPeeps())            ;     If we reach the end of the list,
      NextElement(allPeeps())          ;     wrap around
    EndIf
  Next
  fullGrp()\ctr+1                      ; Up the count of people in the group
  fullGrp()\names+allPeeps()+" "       ; Add the name of the person
  allPeeps()=""                        ; <<-- Just in case of string leakage
  DeleteElement(allPeeps())            ; Remove the person from the people list
  If fullGrp()\ctr=SzGrps              ; Is the group full?
    AddElement(fullGrp())              ; If so, start a new group
  EndIf
Wend

FirstElement(fullGrp())
Repeat
  If fullGrp()\ctr<>SzGrps
    Debug Str(fullGrp()\ctr)+": "+fullGrp()\names+" ***************"
  Else
    Debug Str(fullGrp()\ctr)+": "+fullGrp()\names
  EndIf
  fullGrp()\names=""
  DeleteElement(fullGrp())
  FirstElement(fullGrp())
Until CountList(fullGrp())=0
@}--`--,-- A rose by any other name ..
ravagepn
User
User
Posts: 10
Joined: Sun Mar 06, 2005 3:54 pm

Post by ravagepn »

Here are two ways, #1 with linkedlists:

Code: Select all

NewList mylist.l()
NewList exitlist.l()

pocetak:

ClearList(mylist())
ClearList(exitlist())

For i = 0 To 80
  AddElement(mylist())
  mylist() = i
Next
 
For i = 1 To 80
  brj = Random(79) + 1
  SelectElement(mylist(), brj)
  vst = mylist()
  DeleteElement(mylist())
  AddElement(exitlist())
  exitlist() = vst
Next

OpenConsole()
   
ResetList(exitlist())
While NextElement(exitlist())
  Print(Str(exitlist()) + " ")
Wend

PrintN("")
PrintN("")

ResetList(mylist())
While NextElement(mylist())
  Print(Str(mylist()) + " ")
Wend

While Inkey() = ""
Wend
ClearConsole()  
Goto pocetak
 
CloseConsole()

End 
Way #2 with arrays:

Code: Select all

Dim a(80)

For i = 1 To 80
  a(i) = Random(79) + 1
  For j = 1 To i
    While a(i) = a(j) And i <> j
      a(i) = Random(79) + 1
    Wend
  Next
Next

OpenConsole()

For i = 1 To 80
  Print(Str(a(i)) + " ")
Next

PrintN("")
PrintN("")

PrintN("Press any key")
While Inkey() = ""
Wend

CloseConsole()
  

When you want to re-random list (or array) use goto or procedure commands. Also you can copy list into an array. Something like this :

Code: Select all

Dim broj.l(7)

p = 0
ResetList(exitlist())
While NextElement(exitlist())
  broj(p+1) = exitlist()
  p = p + 1
Wend
I hope I helped a little bit
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Shuffle? To do it you need to remove each number from the selection list as it comes up. May be that's what the examples do above (I'm laZy so I haven't scrutinized the code)
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Post by bidanh00co »

Thank you for all of you ! it' very nice :)

Furthermore, If it can link to an Excel File (which contains list of people Full Name and their index number), it would be great !!
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Oh Excel... may be there is an add-in to randomise a list? I've checked Excels (97) random and sampling but they won't do what you want. Google!
Post Reply