Reply
Need help with random numbers
Old 02-22-2008, 12:23 AM Need help with random numbers
Jaye's Avatar
Novice Talker

Posts: 7
i am working with a children website i wanted text to appear as different color and size for each word in it. but i have all the same values for entire output and different for each page load.

Code:
<%
words="The quick brown fox jumps over the lazy dog"
Dim imgArr, LNum, HNum, RNum,fsArr,fln,fhn,FNum,colorTimer
imgArr = array("#FF0000","#009800","#0000FF","#656666", "#636500", "#9933FF", "#BB00B3", "#066775", "#000000", 

"#FF6700")
fsArr = array("9","10","11","12")
  Randomize
  LNum = 0
  HNum = 9
  fhl=0
  fhn=3
  RNum = INT((HNum - LNum + 1) * Rnd + LNum)
  FNum = INT((fhn - fln + 1) * Rnd + fln)

clr= imgArr(RNum)
fs=fsArr(FNum)

words=replace(words," ","</font> <font face='arial, helvetica' style='font-size: "+fs+"pt' color='"+clr+"'>" & 

vbcrlf)

response.write (words)

%>
Output:
HTML Code:
The</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
quick</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
brown</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
fox</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
jumps</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
over</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
the</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
lazy</font> <font face='arial, helvetica' style='font-size: 11pt' color='#FF0000'>
dog
Jaye is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 02-22-2008, 10:05 PM Re: Need help with random numbers
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
This looks like only part of the code?

Try using Randomize Timer instead of just Randomize. I don't remember how it works in the framework you're using ... if you don't supply a seed, it might default to a constant. Using the timer - specifically, the number of ticks since midnight - means each time the code is run, you'll get a different stream of random numbers.

You can use LBound and UBound for the lower and upper bounds of an array, instead of having to count and hard code the values.

I'm a little confused whether the code you wrote is part of a loop, or if you're running it once? Getting the same results for the color and size every time within the same run looks like something is wrong.

Even from within code, you might be better off using css? If the font face doesn't change, you can assign that once, in a container of some kind. Then you can pick a class name at random, or even two of them, to control the appearance.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 02-22-2008, 11:38 PM Re: Need help with random numbers
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Actually, the Randomize in this case is fine. That's exactly what you're supposed to do to get a random number.

The problem, in this case, is that it's only being called once...period.

You want to do something like this:
Code:
Words = Split (Words, " ") ' This splits your sentence up into the individual words.
Words_Count = UBound (Words)
for i = 0 to Words_Count
'  This is where you put all your random number generating stuff.  Since it's inside a loop, it will be executed once for each word in the sentence.
Next
' I'll leave the rest up to you for output.  This should get you started.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-23-2008, 01:15 AM Re: Need help with random numbers
Jaye's Avatar
Novice Talker

Posts: 7
Thanks Adam you saved my day.
Just in case someone needs it right code is.
Code:
<%
words="The quick brown fox jumps over the lazy dog."
Words = Split (Words, " ") ' This splits your sentence up into the individual words.
Words_Count = UBound (Words)
for i = 0 to Words_Count
Dim clrArr, LNum, HNum, RNum,fsArr,fln,fhn,FNum,colorTimer
clrArr = array("#FF0000","#009800","#0000FF","#656666", "#636500", "#9933FF", "#BB00B3", "#066775", "#000000", 
"#FF6700")
fsArr = array("8","9","10","11","12","13")
  Randomize
  LNum = 0
  HNum = 9
  fhl=0
  fhn=5
  RNum = INT((HNum - LNum + 1) * Rnd + LNum)
  FNum = INT((fhn - fln + 1) * Rnd + fln)
clr= clrArr(RNum)
fs=fsArr(FNum)
words(i)="ƒ" & words(i) & "ô"
words(i)=replace(words(i),"ƒ","<font face='arial, helvetica' style='font-size: "+fs+"pt' color='"+clr+"'>")
words(i)=replace(words(i),"ô","</font> " & vbcrlf)
response.write (words(i))
Next
%>
Jaye is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help with random numbers
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12283 seconds with 12 queries