"How do I make Random the same number each time?"



  • Quote from http://www.experts-exchange.com/Web/Q_22031739.html:

    IntTemp = Int((255 * Rnd()) + 1)
    I used above ASP.NET code. Problem is in " Rnd() "
    Rnd() value is changing everytime.
    What is the alternative for Rnd()? OR How will stop Rnd() value changes at everytime?

    Unbelievably, the best answer was "change the 255 to a 0 and you'll get the same number each time."



  • And this, my friends, is the difference between knowing how to write code, and knowing how to program.



  • I'm sorry, but I don't see it!

     

    Could you just not use 1 or 2 instead of Rnd()?  This is the same as using a non changing number isn't it?  or have I completely misread the entire thing?



  • Do you realize what you're asking here?

    0 * Rnd() will be 0. You add 1 to that 0. This is exactly the same as writing IntTemp = 1
    Indeed, you'll get the same number each time.
     



  • [quote user="TehFreek"]And this, my friends, is the difference between knowing how to write code, and knowing how to program.
    [/quote]

     

    I'm afraid this one is even more simpler than that.

     

    This is the difference of knowing mathematics and not! - or maybe just too many long hours in the office and not seeing the obvious of the routine!



  • Rarely is a website address more ironic than this one...



  • I honestly don't see anything wrong here.

    The response was a bit sarcastic, yes, because the replier thought the asker was a bit of an idiot.

    That said, the asker wasn't. There's nothing wrong with wanting non-random values from a random function. Sometimes you just want a (more or less) consistant data set.

    Obviously the correct answer would have been to set a particular seed value.



  • [quote user="Jivlain"]

    That said, the asker wasn't. There's nothing wrong with wanting non-random values from a random function. Sometimes you just want a (more or less) consistant data set.

    [/quote]

    PLEASE!!! Tell me you're joking.... You want a consistent data set? {1,5,3,4,2} You can rearrange it, if you get bored. Hope it serves you well.

    My 3 ideas:

    1. that person was an idiot
    2. that person was a troll (more probable)
    3. he wanted to make the value random, but permanent through the session (something like sessionId=Rnd()) - likely situation - he didn't know about both sessions and random generator idea

    Anyway - WTF?!?!
     



  • Actually, needing a deterministic pseudo-random series isn't all that uncommon, and you achieve it by seeding the random number generator manually, with the same seed each time. So Jivlain is right about that.

    That said, I have to agree with viraptor that the person asking that question most probably has no clue what Rnd() actually does, judging from the message.

     



  • I'm going to agree with viraptor here.  If you want a data set that doesn't change, but you don't care what the numbers are, hire a monkey to hit some keys.  Relying on the side effects of Random Number Generators (RNG) is equivalent to relying on a bug.  A RNG is not designed to give you a sequence of numbers that will be the same across runs.  The fact that you can make it do that is a side effect.  Think of it as using a screwdriver to carve wood.  You can certainly do it, but since the screwdriver was not designed to carve wood, you will never make anything worth my money.  And if someone swaps your flat head for a phillips, things get worse.  What happens if someone puts in a different RNG that has a different sequence from the same seed?  Things become inconsistent and you'll have a horrible time debugging it.  What happens if someone puts in a new RNG that doesn't have that side effect (like a RNG that determines the next value by heat on the processor, the phase of the moon, and the direction of the wind)?  Bottom line:  This is definitely a WTF.



  • Seems like someone doesn't know anything.

    http://www.experts-exchange.com/QH_3705788.html 



  • Cotillion, you can read about the basics of PRNGs here if you are interested.



  • Christ, the need for a random number to stay consistent throughout the session could be useful, sure, I could even buy that somebody might want a random number to stay consistent forever throughout all sessions, but to say that "whoa Rnd() * 0 magically gave me the same number all of the time" is just plain ignorant. 0 times whatev will always be 0. That value is then increased by 1. Conclusion: if i assign IntTemp a value in the following manner: IntTemp = 1, then IntTemp will always have the value of 1. *GASP*


    I really can't find it in me to believe that this is what the asker really wanted, because even the worst programmer (yes, even the worst, don't argue) would just write IntTemp = 1, if that was the desired effect. 



  • Wow no. You should NEVER reply on the output on a Rnd() function to be the same all the time by giving it a known seed.

    Unless explicitly stated in the documentation that this is an intended use (in which case... try a different framework. The one you're suing sucks), that's just asking for trouble.  Someone finds a flaw in Rnd() and fixes it, or the decision is just made to move to a faster/more random/better/whatever version, and your code breaks.

    Relying on a fixed predetermined output from a function intended to give a random output isn't just stupid, it's (hopefully) obvious to pretty much everyone in the Universe that it's stupid.
     



  •  

    This may sound kind of dumb, but maybe he/she thought Rnd() was "Round" instead of "Random"?

     

    Could be wrong though



  • [quote user="RayS"]

    Wow no. You should NEVER reply on the output on a Rnd() function to be the same all the time by giving it a known seed.

    Unless explicitly stated in the documentation that this is an intended use (in which case... try a different framework. The one you're suing sucks), that's just asking for trouble.  Someone finds a flaw in Rnd() and fixes it, or the decision is just made to move to a faster/more random/better/whatever version, and your code breaks.

    Relying on a fixed predetermined output from a function intended to give a random output isn't just stupid, it's (hopefully) obvious to pretty much everyone in the Universe that it's stupid. [/quote]

    You are totally misunderstanding the issue here.  People aren't expecting a certain set of values for a given seed.  They simply expect the same output as last time for a given seed.  There are many examples where this is useful.  Want to randomize a deck of cards them same way as a previous hand?  Set the seed to the same value.  Want to test a Monte Carlo simulation between to different builds?  Use the same seed.

    If a PRNG ever gives a different series of numbers for the same seed, then it contains a serious bug.  It would imply that it is neither pseudo random, or that it is following different code paths for the same input.

    Remember, rand() is random.  It is pseudo random.  Why do you think you have to supply a seed, anyways? 



  • [quote user="jkandrach"]

    This may sound kind of dumb, but maybe he/she thought Rnd() was "Round" instead of "Random"?

    Could be wrong though[/quote]

    I think you might be right.  But given that Rnd() doesn't have any parameters, I have to wonder what he thought might get rounded? 



  • Maybe I'm just not jaded enough, but I have a hard time believing a question this ridiculous is anything but a half-assed trolling attempt.

    Please world, don't prove me wrong.
     



  • google that screenname "genesiscircle".

    "GenesisCircle is a computer software development company with our main development office located in South India. "



  • [quote user="jes5199"]

    google that screenname "genesiscircle".

    "GenesisCircle
    is a computer software development company with our main
    development office located in South India. "

    [/quote]

    That would explain the Who We Are page... 



  • hehehe thats the funniest about us page I've seen in months:

    "She is now the Administrator of our India development center. We all like to call her as 'Sister'"

    "During her free time, she likes to spend time with her kid."

    "Liya's interests include Tasting of new recipes, Reiligious thoughts & Friends"



  • [quote user="RayS"]

    Wow no. You should NEVER reply on the output on a Rnd() function to be the same all the time by giving it a known seed.[/quote]

    Actually, it is very common to do this with a pseudo-random number generator. As long as you KNOW that the offending function is a proper pseudo-random number generator, it's fine. Many networked games do this sort of thing for 'random' events (say, card decks), so that both sides stay in sync without constant synchronisation.


Log in to reply