This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

help with random number proc

Old posts that have not been replied to for several years.
Locked
b
blotter45
Voice
Posts: 17
Joined: Mon Feb 28, 2005 8:37 am

help with random number proc

Post by blotter45 »

Hiya ;)

I'm trying to make a proc to output a string of random numbers, based on a given limit.

Code: Select all

bind dcc o|- otrandset ot:randset

#number of random questions to pick each time a test is begun
set ot_var(qlimit) "10"

proc ot:total { } {
  # returns the number of total questions
  global ot_q
  return [array size ot_q]
}

proc ot:randset { hand idx arg } {
  global ot_var
  if {![valididx $idx]} {
    return 0
  }
  set tot [ot:total]
  set i ""
  while {[llength [join $i]] <= $ot_var(qlimit)} {
    set r [rand $tot]
    if {[lsearch [join $i] $r] == "-1" && $r != "0"} {
      set i [lappend i $r]
    } else {
      continue
    }
  }
  putdcc $idx "[llength [join $i]] random numbers: \'$i\'"
  return 0
}
So for example if there are 30 total questions, that proc should output a list of 10 random question numbers, but only if the number isn't already in the list, and only if the number is not zero. But the above proc just hangs when run.. Any suggestions?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

dont use [join ] on $i, llength is list command, lappend add a list to a list, so no need to convert the list to a string. the only time join makes sense is at the very last '$i' (no need to escape single quotes, they don't have special meaning in TCL), because this is the only time you need your list as string. But since your list contains only numbers as elemtes there should be no diffrenz. but with a bit bad luck llength returns always 1 (which would be always below 10). In my test it at least doesn't.
[14:08:54] tcl: evaluate (.tcl): llength [join {10 20 1 400}]
Tcl: 4
second, you don't need use "" for numbers, but this should be problem:
[14:12:17] tcl: evaluate (.tcl): expr -1 == "-1" && 1 != "0"
Tcl: 1
lets come to "set i [lappend i $r]"
Your are writing 2 times to the same var. but this, too should be no problem:
[14:29:47] tcl: evaluate (.tcl): set test 1
Tcl: 1
[14:30:03] tcl: evaluate (.tcl): set test [lappend test 2]
Tcl: 1 2
So I see 3 things that look suspicious, but none I can confirm to be the problem.
Last edited by De Kus on Sat Mar 19, 2005 9:34 am, edited 1 time in total.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Something like this should do it:

Code: Select all

set number 0;
set list_of_numbers [list];

while {$number <= 10} {
 set temp_number [rand 31];
 if {([lsearch -exact $list_of_numbers $temp_number] != -1)} {
  lappend list_of_numbers $temp_number
  incr number
  }
}
or you can use llength to:

Code: Select all

set list_of_numbers [list];

while {[llength $list_of_numbers] <= 10} {
 set temp_number [rand 31];
 if {([lsearch -exact $list_of_numbers $temp_number] != -1)} {
  lappend list_of_numbers $temp_number
  }
}
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Locked