Search Unity

Not repeated random number(Random.Range)

Discussion in 'Getting Started' started by subitdhakal1234, May 15, 2020.

  1. subitdhakal1234

    subitdhakal1234

    Joined:
    Nov 25, 2019
    Posts:
    15
    Hello, I am new to programming and I am getting repeating number and get stucked.



    public void PlayerSelected(Player selectedPlayer)
    {
    Vector3 bottomLeftWorld = Camera.main.ViewportToWorldPoint(new Vector3((float)0.2, (float)0.1, Camera.main.nearClipPlane));
    Vector3 bottomRightWorld = Camera.main.ViewportToWorldPoint(new Vector3((float)0.4, (float)0.1, Camera.main.nearClipPlane));
    Vector3 initantiatePos = new Vector3(Random.Range(bottomLeftWorld.x, bottomRightWorld.x), bottomRightWorld.y, 0);
    player = selectedPlayer;
    Player newPlayer = Instantiate(player, initantiatePos, Quaternion.identity) as Player;
    }
     
  2. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    You'll need a seed.
     
  3. subitdhakal1234

    subitdhakal1234

    Joined:
    Nov 25, 2019
    Posts:
    15
    I dont know
    how to do bcz i am really noob in programming
    buddy could you please help me
    by writing the complete code .
    from the above code . Please @ikazrima
    u will be very thankful
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    First up, this is in the wrong place. "General Discussion" is specifically not a support area, and we have a "Scripting" section exactly for this.

    Secondly, we need a bit more information. What do you mean you are "getting repeating number"? Do you mean it's always the same number, or that you get occasional repetitions which you want to avoid? Or is it something else?

    That's... not really how it works around here.
     
    Socrates and EternalAmbiguity like this.
  5. subitdhakal1234

    subitdhakal1234

    Joined:
    Nov 25, 2019
    Posts:
    15
    Sorry ! I was not aware .
    its not always the same number repeating over and over
    I am getting the occasional repetitions that i want to avoid .
    because the GameObject instantiate in the same position where the previous game object appear
    Please help me
    I dont know where to get the help . bcz I am new member in this forum.
    Next time I will appeal for help in "Scripting" section.Thank you..
     
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,151
    What you are looking for is traditionally called a "shuffle bag." Here is a short tutorial on how they work.

    https://gamedevelopment.tutsplus.co...-making-random-feel-more-random--gamedev-1249
     
    angrypenguin likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Another approach is to check distance against existing spawned players, and either adjust the position or pick a new one if they're too close.
     
  8. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    Depends on where the camera is. The random position is on the Camera.main.nearClipPlane. If 0 is far away (the z instantiate position), the position may be random, but the variation is so small it always looks the same.